command line - Delete a file inside subfolder using bat

06
2014-04
  • Deb

    I know the name of the subfolder inside which the file I want to delete is located. The name of the subfolder is "My Subfolder" and the file I want to delete inside the subfolder is "The-Bad File.txt" . I am at "D" drive. "My Subfolder" is located inside D:\Folder-2\ ; D:\Folder-4\; D:\Folder-54\; D:\Folder-543\etc etc (I don't know the actual folder names inside which "My Subfolder" is). I want to search these folders only to delete "Bad File.txt" using .bat file.

    I don't want this : -

    @echo off
    del /s "Bad File.txt"
    

    This will search the entire drive, which will take time. What I want in more perfect word is, I want to search for "My Subfolder" First. Then Inside "My Subfolder" want to delete the "Bad File.txt"

  • Answers
  • Multiverse IT

    If you're ABSOLUTELY SURE there is only one instance of My Subfolder, then:

    @echo off
    Set StartFolder=D:\Path\To\A\Folder
    for /f "tokens=*" %%a in ('DIR /s /b /ad "%StartFolder%"') Do del "%%a\Bad File.txt"
    

    Just change the path for "StartFolder" on line two to the folder you want to start looking in.

    Though I really don't think searching for the folder is going to improve the time it takes (not by much at least).


  • Related Question

    windows vista - bat file for backup system
  • tugberk

    I created a sample bat file in order to have a back-up system. this does not show what my intention is but it demonstrates it. here is my bat file content;

    xcopy C:\apps\source c:\apps\dest /D /E /C /R /H /I /K /Y
    

    what I want is this;

    I want to copy every updated file and sub-folders from source folder to dest folder. is this code is ok for that. thanks !


  • Related Answers
  • Linker3000

    Xcopy's performance and features leave a lot to be desired compared to the alternatives.

    Have a look at Richcopy - it's Robocopy on steriods and also from Microsoft. Richcopy is also Multithreaded (Robocopy only became multithreaded with the Windows 7 version) and so it's throughput is quite impressive.

    http://technet.microsoft.com/en-us/magazine/2009.04.utilityspotlight.aspx

    http://www.servermigrator.com/blog/2009/08/11/the-easiest-way-to-prepare-richcopy-command-line-parameters/

  • Biglig

    You need /S /E rather than /E on it's own.

    And if it's a backup system, you probably don't want to supress errors with a /C, but to react to them.

  • Joshua

    I recommend using robocopy, which is part of Windows 7 / Server 2008. More information here: http://en.wikipedia.org/wiki/Robocopy

    This tool provides a bunch of options, not the least of which is an ability to "mirror" your locations and copy only the changes (so you don't have to do the entire file dump again, which saves a LOT of time if you are dealing with large or large numbers of files).

    Example of mirror, copying only changes:

    robocopy C:\data E:\data /MIR /Z