This time I am presenting three methods to do a common deletion task.
They all use the same method for the deletion, but each additional version
adds a capability to the previous.
The initial idea is to delete all files in the current directory. Typically, this would be done via:
... and then the user would press `Y' and `Enter' to the "All Files Will be Deleted" message and "Are You Sure?" prompt. Here are three methods that accomplish this, but with less work involved.
1. To simply delete all files in the current directory without prompts:
ECHO Y | DEL . > NUL
This echoes (sends) a "Yes" answer to the DOS prompt of "Are you Sure" when DELETE is used to remove all files. The `.' is a short name for the current directory, and sending the whole works to NUL means no message and prompt are ever seen. So this method means nothing appears on the screen and no extra keystrokes are required.
If you wish, place this into a batch file and add a line with "DIR" to get a confirmation that the files have been deleted. So with one command, all files can be quickly deleted and a directory listing confirms the action.
:: DEL-FILE.bat @ECHO OFF ECHO Y | DEL . > NUL DIR
To make this very powerful, assign the DEL-FILE batch file to a keyboard macro. I use "Control-F". One key combo and all the files are gone with no backtalk!
2. Suppose though, that you wish to delete everything except one file?
:: DELE.bat (Delete Except, Batch File) @ECHO OFF MD SAVE XCOPY %1 SAVE > NUL ECHO Y | DEL . > NUL MOVE SAVE\*.* . > NUL RD SAVE DIR
This batch file makes a temporary "SAVE" directory and copies the file that is not to be deleted to it. ("%1" represents the file name you type at the command line after the batch file name.) Then as in the first example, all files in the current directory are deleted showing no screen output. The excepted file in returned to the current directory and the "SAVE" directory is removed. Finally, DIR displays the results of the operation.
3. Now suppose that you want to exclude muktiple files from the deletion operation:
:: DELE.bat @ECHO OFF MD SAVE :COPY-FILES XCOPY %1 SAVE > NUL SHIFT IF NOT "%1" == "" GOTO COPY-FILES ECHO Y | DEL . > NUL MOVE SAVE\*.* . > NUL RD SAVE DIR
This version works as before but after the first file listed at the command line has been copied, the SHIFT command is used. SHIFT moves all command-line parameters down by one position so that the second file listed becomes the first. Next, the IF NOT line is used to see if there is a file listed in the first position. If there is, the batch file is directed via GOTO to the label COPY-FILES. DOS knows it's a label because it begins with a colon.
XCOPY copies that file to the SAVE directory and then the batch file shifts the parameters downward once again. This continues until there are no more parameters in the first position, at which point the IF NOT becomes false. Now, the files are deleted in the cvurrent directory and the excepted files are returned. The SAVE directory is deleted and DIR displays the current directory with the files you chose to exclude from deletion.
The Power is at the Command Line!
Richard Bonner is a DOS power user and has his own DOS website:
The DOS Operating System