Deleting folders using batch files




















Viewed k times. I want to delete a folder with all files and subfolders using a bat file. Improve this question. Add a comment. Active Oldest Votes. Used to remove a directory tree.

Improve this answer. Cristian Ciupitu Jon Jon k 74 74 gold badges silver badges bronze badges. Can you please explain, why the flag befor RD ist needed and used? In the explanation there is only with rd. Change the root folder on the top line and it's ready to run. Also, you could take a different approach and simply remove and recreate the root folder like this. Todd for some reason that didnt delete anything. I tested it on Windows 7 and it worked to delete files and folders.

What Windows version are you trying it on? You will have to write a for loop probably to first get a list of all the sub directories one level down from the top and then iterate through the list issuing the RD command.

Unless the directory names are static in which case you could just use one line for each directory. That may be the difference. The effect is the same. Hmm, Todd's idea is much easier and cleaner I think. It's a lot less complicated to delete the root folder and then just re-create it. Seems like there are so many ways to do this but here is another method that works for what I am looking for but it only works for one directory.

ANyone know how to edit this for multiple directories? Ostertag I see your edit! Name it BatchDelete and open it. The batch file example requires you to know which directory you want to delete the file in. This sounds obvious, but you need the exact file path of the directory. If you are unsure of the correct path for the folder, right-click and choose Properties , see the location there. Or, browse to the folder and click once in the address box to show the directory path directly.

You can copy and paste the following into your batch file. You should replace "enteryourpathhere" with the directory path.

Now, find your BatchDelete. Instead of typing in a specific file's name, you'll type in a quotation mark, an asterisk, the extension, and another quotation mark. This tells your computer to delete any files ending in your specified extension.

Add more file types if necessary. Save your file as a Batch file. Do the following: Click File. Click Save As Type whatever you want to name your Batch file followed by.

Click the "Save as type" box, then click All Files. Select a save location on the left side of the window, then click Save. Run your file whenever you need to clear the folder. Simply double-click the Batch file to do so. This will remove any file s matching your specified extension s from the folder. If you ever move the folder, you'll need to edit the Batch file to include the updated folder's address. Emptying a Folder of All Files Understand how this works.

If you want to remove an entire folder's file contents without deleting the subfolders inside of the main folder, you can use a Batch file to do so. This method won't delete folders inside of the directory, but it will remove all files from those folders.

Right-click the folder you want to clear. This will open a drop-down menu. Doing so opens the Properties window. Copy the folder's location. Enter the "Change Directory" command along with the folder's address.

Enter the command to delete all files without confirmation. Paste in the folder's address from earlier. Type in the folder's name exactly as it appears in the address bar when you open the folder. Run your Batch file when needed.



0コメント

  • 1000 / 1000