Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
MillenX

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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).

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...