February 7, 201016 yr can i create a command/batch that, in a folder that contains subfolders of many levels, automatically delete all the files that are named as the corresponding folder and subfolders?For example, In my pendrive, there're folders A and B, B has folders C and D.I want to delete A.exe, B.exe, C.exe and D.exe in the corresponding folders, by launching a command/batch file in the root of my pendrive or in the root of A.It's a virus.
February 7, 201016 yr Sure something like thisdel /s a.exe,b.exeorfor /r %G in (a.exe,b.exe) do del %GFOR /R [[drive:]path] %variable IN (set) DO command [command-parameters] Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree.
February 8, 201016 yr I read it differently, therefore this will delete A\A.EXE, B\B.EXE, C\C.EXE and D\D.EXE.@FOR %%# IN (A B C D) DO @IF EXIST %%#\%%#.EXE DEL/A/F %%#\%%#.EXE
February 8, 201016 yr It's my assumption that those filenames were for descriptive purposes only. I'm waiting for him to come back and say that it is not restricted to single letter files/folder names (or even names without spaces for that matter) and cannot be hardcoded as such. My guess is his intentions were that it would read the folder name, then if a file exists matching the foldername +".exe" format delete it, yada yada yada. Is that what you were really asking for ? Does it have to be a batch file or would a VBS script be acceptable ? Edited February 8, 201016 yr by MrJinje
February 8, 201016 yr In that case, this should suffice:@FOR /F "TOKENS=*" %%# IN ('DIR/B/S/AD') DO @IF EXIST "%%#\%%~n#.EXE" DEL/A/F^ "%%#\%%~n#.EXE"Copy as viewed (on two lines, the first ending with a circumflex).
February 13, 201016 yr Author It's my assumption that those filenames were for descriptive purposes only. I'm waiting for him to come back and say that it is not restricted to single letter files/folder names (or even names without spaces for that matter) and cannot be hardcoded as such. My guess is his intentions were that it would read the folder name, then if a file exists matching the foldername +".exe" format delete it, yada yada yada. Is that what you were really asking for ? Does it have to be a batch file or would a VBS script be acceptable ?yes, this is exactly what i am saying..those filenames are just for descriptive purpose.. sorry for not informingas long as it can delete them, a vbs will do, though i prefer batch moreThanks MrJinje Edited February 13, 201016 yr by MillenX
February 13, 201016 yr Author In that case, this should suffice:@FOR /F "TOKENS=*" %%# IN ('DIR/B/S/AD') DO @IF EXIST "%%#\%%~n#.EXE" DEL/A/F^ "%%#\%%~n#.EXE"Copy as viewed (on two lines, the first ending with a circumflex).Thanks! it works!What does "TOKENS=*" means?
February 13, 201016 yr What does "TOKENS=*" means?It means ALL tokens.Here:http://www.robvanderwoude.com/batchfiles.phphttp://www.robvanderwoude.com/for.phphttp://www.robvanderwoude.com/ntfor.phphttp://www.robvanderwoude.com/ntfortokens.phpjaclaz Edited February 13, 201016 yr by jaclaz
February 13, 201016 yr This is a way to do what you wanted as a VBS script. To use this script just Drag And Drop The Folder on the script.Save As DelFileNameAsFolder.vbsDim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Obj If Wscript.Arguments.Count => 1 Then For Each Obj In WScript.Arguments If Right(InStr(Obj,"."),5) Then '-> Do Nothing It A File Else Recursive(Fso.GetFolder(Obj)) End if Next Else'-> This Is For If You Dont Want To Use Drag And Drop UnComment What You Need'-> If You Want To Hard Code A Path UnComment Below And Add Path ' Recursive(Fso.GetFolder("DRIVE\PATH_FOLDER1\FOLDER"))'-> Or Leave The Script In The Folder And UnComment Below ' Recursive(Fso.GetFolder(".")) End If '-> Function To Go Threw Directories And List Files Function Recursive(Folder) Dim File, Col For Each File In Folder.Files If InStr(1,File.Name,Folder.Name,1) Then Fso.DeleteFile(File.Path),True Next For Each Col In Folder.subFolders Recursive(Col) Next End Function
Create an account or sign in to comment