-->
Searching...
Thursday, July 13, 2017
11:04 PM

Delete a list of files using command line in Windows

“Talk is cheap. Show me the code.” ― Linus Torvalds


I needed to delete a bunch of files today and because I was lazy decided to use a command line to delete a list of files but I want to keep the folders I just want to delete its content and since there was like 200 folders, command is the faster option rather than clicking one by one.

Since I find it very useful, I decided to write this little tutorial.

Here is my way of doing it.

First I generate a name list of all the folders I wanted to delete. (To open command line windows in a specific folder press "shift+right click > Open command window here", this is easier than directing the command window path using CD)

To generate a text list of all the files in a directory type:
dir /b >"namelist.txt"

This will create a file 'namelist.txt' containing the name of files in that particular directory. /b is to produce bare format, without /b it will list the date, time and size of the files

I did some minor editing to remove files that I want to keep its content.

Then here is the fun part

To delete all the content from that list type:
for /f %i in (yourfolderdirectoryhere\namelist.txt) do del /q %i\*

So the command will check namelist.txt, read in one by one and delete its content. If you want to delete the folder itself then remove *

If you are writing this as a .bat file to run through command console add extra % to the %i variable.
for /f %%i in (yourfolderdirectoryhere\namelist.txt) do del /q %%i\*

/f is to force delete even the the read only file
/q is for quite mode, it wouldn't ask you whether its okay to delete.

There all done. Now I have bunch of empty folders. “The most disastrous thing that you can ever learn is your first programming language.” ― Alan Kay

0 comments:

 
Back to top!