Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete N files of X type from Y subfolders from a Windows batch file?

I'm trying to write a windows batch file that can delete files from subdirectories. I would rather not hard code the directory structure in, so I can use this process with other projects.

  • I need to delete files of X type,
  • I have the parent folder C:\MyProject,
  • There are Y subfolders C:\MyProject\?,
  • There are N files to delete.

Is there a quick del (of type) function I am simply missing?

like image 969
Jeremiah Avatar asked Dec 03 '08 15:12

Jeremiah


People also ask

How do I remove all files from a subfolder?

CD [Your_Folder] && RMDIR /S /Q . From Windows Command-Line Reference: /S: Deletes a directory tree (the specified directory and all its subdirectories, including all files).

How do I delete the contents of multiple folders?

You can delete multiple files or folders by holding down the Ctrl key and clicking each file or folder before pressing Delete . You can hold down the Shift key while pressing the Delete key to prevent files from going to the Recycle Bin when deleted.

How do I remove all files from a directory in DOS?

The command DEL /F/Q/S *. * > NUL deletes all files in that folder structure, and omits the output which improves the process further. Use cd.. to navigate to the parent folder afterwards. Run the command RMDIR /Q/S foldername to delete the folder and all of its subfolders.


1 Answers

Actually you can use the standard del command:

c:
cd MyProject
del /S *.type

Where type is the extension you want to delete and the /S parameter will check in all subfolders of MyProject.

like image 114
Pedrin Avatar answered Oct 09 '22 19:10

Pedrin