![Write Batch File To Delete Files In A Folder Write Batch File To Delete Files In A Folder](http://www.get-itsolutions.com/wp-content/uploads/2016/06/Batch-delete-file.jpg)
It seems you’d need to make a for loop inside the batch file to remove each subfolder individually. This seems to work: @echo off del /Q '%1.' for /d%%d in (%1.) do rd /S /Q '%%d'.%1 is a reference to the 1st argument you send to the bat file - i.e. The path which contains the stuff you wish to delete.
The process will vary depending on what you'd like to do. Ifyou'd just like a simple way to delete all of a given extension andpause, you could write the script:del./.temppauseTo delete an entire folder, you can use the rmdir (ReMoveDIRectory) command. A similar script that would delete anythingending in '-2006' could be written like so:rmdir./.-2006Batch scripting can be very simple or very complicated dependingon what you're asking of it. It is hard to give advice withoutknowing exactly what needs to be done. For more information on yourspecific problem, try using your favorite search engine to findonline articles about batch scripting.
I just tested this on Windows 7 and it works fine. Change the root folder on the top line and it's ready to run.SET rootfolder=c:FTAPPIF exist%tmp%folderlist.txt del%tmp%folderlist.txtDIR%rootfolder% /A:D /B %tmp%folderlist.txtCD /d%rootfolder%FOR /F%%i IN (%tmp%folderlist.txt) DO RD%%i /Q /SAlso, you could take a different approach and simply remove and recreate the root folder like this.SET rootfolder=I:FTAPPRD%rootfolder% /Q /SMD%rootfolder%Edited May 22, 2013 at 13:37 UTC. I just tested this on Windows 7 and it works fine. Change the root folder on the top line and it's ready to run.SET rootfolder=c:FTAPPIF exist%tmp%folderlist.txt del%tmp%folderlist.txtDIR%rootfolder% /A:D /B %tmp%folderlist.txtCD /d%rootfolder%FOR /F%%i IN (%tmp%folderlist.txt) DO RD%%i /Q /SAlso, you could take a different approach and simply remove and recreate the root folder like this.SET rootfolder=I:FTAPPRD%rootfolder% /Q /SMD%rootfolder%Edited May 22, 2013 at 13:37 UTC. Ostertag I see your edit!
Ok option 2 looks good but how do I do this as one bat file to cleanup multiple sub folders? The root has the 4 folders in my OP. Basically I want to set root folder to my 'i' drive (USB drive) and to delete everything in the ftapp, ftapp2, ftadmin, and ftdc1 folder but not to delete those folders. Maybe I have to make 4 bat files and 4 seperate Windows scheduled tasks to run at the same time? THanks againOption 1 does not work I run as administrator and can see many access denied errors.
Be careful when using USB drives as the drive letter could change. I usually add something to the root of the drive so the batch script and positively identify the drive letter. For instance I might just add a file to the root of the USB drive and name it usbdrive.id Then I have the batch script look for that file first.
That way if it can't find the drive or a different drive is attached, we can have the batch script just stop or alert me via email or something. You get the idea. We have remote employees on laptops. They each have an encrypted USB hard drive used for backing up their profiles.
Sometimes when the drive is inserted, it jumps to a different letter. To get around that, I wrote a batch script that checks all drive letters and looks for the drive id file. When it finds it, it set the drive letter as a variable and then uses that variable as a target in the rest of the script. Just something to think about.Back to your original question, the I think the simplest solution is usually the best.
Considering that the four target directories are 'fixed', I would probably use option two and just right 4 lines for removal and 4 for creation.RD I:ftapp /Q /SRD I:ftapp2 /Q /SRD I:ftadmin /Q /SRD I:ftdc1 /Q /SMD I:ftpappMD I:ftapp2MD I:ftadminMD I:ftdc1. I am using this to delete multiple files in two different drives, but it is executing only for the first c drive and exiting without working on D drive, any thoughts?c:cd tempforfiles /s /m.dmp /c 'cmd /c del @file'forfiles /s /m javacore. /c 'cmd /c del @file'forfiles /s /m heapdump. /c 'cmd /c del @file'D:cd binforfiles /s /m.dmp /c 'cmd /c del @file'forfiles /s /m.phd /c 'cmd /c del @file'forfiles /s /m.trc /c 'cmd /c del @file'forfiles /s /m javacore.
/c 'cmd /c del @file'.