Jump to content

special delete command help


Recommended Posts

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


Sure something like this

del /s a.exe,b.exe

or

for /r %G in (a.exe,b.exe) do del %G

FOR /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

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 by MrJinje
Link to comment
Share on other sites

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

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 informing

as long as it can delete them, a vbs will do, though i prefer batch more

Thanks MrJinje

Edited by MillenX
Link to comment
Share on other sites

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

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.vbs

Dim 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...