Deleting files in command prompt using wildcards is quite straight forward.
Command below will delete all text (".txt") files on the specified path.
Command above will delete all files with ".txt" extension in d:\txtlog directory.
Easy enough to delete all matching files.
Using the same method with rmdir or rd command this will not work.
For example, if we have a directory on d drive that is auto-generated by an application and the filename is consistent with a pattern plus incrementing number at the end to differentiate the folder from other folders.
The folder name has a consistent pattern that is preceded by the word “log” plus incrementing number.
If the command below is executed to remove the directories in one go, an error is shown which has this message: "The filename, directory name, or volume label syntax is incorrect."
rmdir D:\baklogs\log* /s
rd D:\baklogs\log* /s
/s - recursive deletion all folders and subfolders
The method above will result in an error because the command interprets the input literally, the command is looking for a folder with this folder name "log*".
Deletion of directories using wildcard can be done with some twist.
In order to remove a directory, a complete path is needed as input to rmdir or rd command.
Input requirements of "RD" or "RMDIR" command can be meet, using Dir command and the right use of its parameters.
Dir command which lists files and folders and provides an option to display the folder in a complete path.
dir /a:d /s /b l*
Command above will display directories that start with letter "l" including its path.
The input requirement for rd or rmdir command is completed using the dir command.
In order to delete all the directories that match the wildcard; for loop is needed and supplied to the rd or rmdir command.
Here's the script that will do the job.
for /f %i in ('dir /a:d /s /b l*') do echo rd /s /q "%i"
That's it a dir command and for loop will be able to delete directories recursively using wildcards.
Above command uses "echo" to show the directories that will be deleted.
Removing the "echo" will permanently remove all the directories that match the wildcard, before removing ”echo” pray hard that you're doing the right thing.
Cheers..till next time :)
Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet:
Catholic Rosary Guide for Android:
Divine Mercy Chaplet Guide (A Powerful prayer):