Chris Rasmussen · Photographer · Infrastructure Guy · Code Dabbler · Traveller

Old school DOS command for file clean-up

It’s time for a bit of a step back now – to the days of DOS commands. Let’s say you want to delete all files matching a file pattern. What do you do? I’m talking about something slightly more useful and complex than ‘del *.*’ …

With Windows Explorer hanging about nowadays the solution might seem pretty simple. Just do a file search on the drive or folder you want to clean up and delete the files it finds. Ok, that might work. What if you want to exclude certain folders? What if you want to schedule the job? What if you want to script it? Luckily DOS provides the ‘for’ command that you can use for this. Consider the following directory structure.

The folders above contain the following files.

You can see that each folder contains some .TXT files, some .ZIP files and a couple of sub folders. In this example we want to delete all the .ZIP files (they’re archives) but we also want to keep a list of the files that were deleted as well as a log file of the commands that were run to delete them. We also want to replace the files with a new folder that matches the name of the .ZIP file but without the .ZIP extension. Sure, you could write a PowerShell or VB script to do this but you can’t get it done with a single line. With DOS ‘for’ commands, however, you can. Here’s how.

for /D %d in (device*.*) do for %z in ("%d\*.zip") do @echo mkdir "%d\%~nz" >> .\cleanup_commands.txt | mkdir "%d\%~nz" | @echo %z >> .\cleanup_files.txt | @echo del "%z" >> .\cleanup_commands.txt | del "%z"

WTF you might be thinking? That looks like a load of rubbish. The first thing to know is that if you want to run the above command from a batch file you need to replace all occurrences of % with %% – do that now if you’re adding the command to a batch file. Here’s what each part means, assuming you’re running the command from the directory you want to clean up.

for /D %d in (device*.*) – Go through all sub-directories in the current directory (/D indicates that the command should look at directories only)
do for %z in (“%d\*.zip”) – For each of the sub-directories found in the command above go through the list of all files with a .ZIP extension
do @echo mkdir “%d\%~nz” >> .\cleanup_commands.txt – For each of the .ZIP files found echo the command that would create the new directory that matches the name of the .ZIP file *without* the .ZIP extension to a file called cleanup_commands.txt in the currect directory
| mkdir “%d\%~nz” – For each of the .ZIP files found create a new directory in the appropriate sub-directory that matches the name of the .ZIP file *without* the .ZIP extension
| @echo %z >> .\cleanup_files.txt – For each of the .ZIP files found echo the name of the .ZIP file to a file called cleanup_files.txt in the current directory
| @echo del “%z” >> .\cleanup_commands.txt – For each of the .ZIP files found echo the command that will delete the file to a file called .\cleanup_commands.txt in the current directory
| del “%z” – Delete the .ZIP file

And now here’s a more granular explanation of what certain parts of each command mean.

for /D – For each directory ONLY
%d – The variable that will refer to each directory later
(device*.*) – Only deal with directories that match the pattern device*.*
%z – The variable that will refer to each .ZIP file later
(“%d\*.zip”) – Only deal with files that match the pattern device*.*\*.zip
“%d\%~nz” – %~nz means only return the name of the file WITHOUT the file extension

Note – the | (pipe) symbol combines more than 1 command onto a single line in DOS. The quotes are also important because in the example above the directory names contain spaces.

3 Responses to “Old school DOS command for file clean-up”

  1. Ross says:

    OK I’ll bite :)

    It’s not really a ‘single line’, is it? It’s many lines concatenated into one…

    And also your use case is a bit unusual. I find for simple archive / deleting tasks (eg find all files over x days old and move them to a network share / zip them / delete them / whatever) the DOS ‘forfiles’ command works wonders. Between that and Robocopy, there’s not much that can’t be done ;)

  2. Chris says:

    Technically no it’s not a single line but it IS a single command. The concatenation is kinda required in this case though – without it you can’t easily take the results of the previous command and pipe it into the next.

    Good comment about ‘forfiles’ though. I would’ve used it but didn’t even think about it at the time! :)

    (As a side-note all I can say is look at the scripts on TMMOFO1 haha)

  3. ms dos 2.0 says:

    [...] for DOS are released. September: Microsoft Excel 1.0 for Macintosh is released. It is designed …Old school DOS command for file clean-up | Digital FormulaHow to use a DOS command to perform multiple actions with 1 command – used for file cleanup … .net [...]

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes