MillenX Posted February 7, 2010 Share Posted February 7, 2010 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. Link to comment Share on other sites More sharing options...
DigeratiPrime Posted February 7, 2010 Share Posted February 7, 2010 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. Link to comment Share on other sites More sharing options...
Yzöwl Posted February 8, 2010 Share Posted February 8, 2010 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 Link to comment Share on other sites More sharing options...
MrJinje Posted February 8, 2010 Share Posted February 8, 2010 (edited) 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, 2010 by MrJinje Link to comment Share on other sites More sharing options...
Yzöwl Posted February 8, 2010 Share Posted February 8, 2010 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). Link to comment Share on other sites More sharing options...
MillenX Posted February 13, 2010 Author Share Posted February 13, 2010 (edited) 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, 2010 by MillenX Link to comment Share on other sites More sharing options...
MillenX Posted February 13, 2010 Author Share Posted February 13, 2010 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? Link to comment Share on other sites More sharing options...
jaclaz Posted February 13, 2010 Share Posted February 13, 2010 (edited) 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, 2010 by jaclaz Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 13, 2010 Share Posted February 13, 2010 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 Link to comment Share on other sites More sharing options...
MillenX Posted February 16, 2010 Author Share Posted February 16, 2010 Cool! Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now