Skip to main content

Delete Directories with Wildcards using rd or rmdir

 
Deleting files in command prompt using wildcards is quite straight forward.

Command below will delete all text (".txt") files on the specified path.

     Del D:\txtlog\*.txt

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.


   D:\baklogs\log1\
   D:\baklogs\log2\
   D:\baklogs\log3\
   Etc..
   D:\baklogs\log100\


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.

Example:
   D:\baklogs\log1\lrx

   D:\baklogs\log2\lrb

   D:\baklogs\log3\lbr

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:
https://play.google.com/store/apps/details?id=com.LinuxMobileKit


https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer

Catholic Rosary Guide  for Android:

https://play.google.com/store/apps/details?id=com.myrosaryapp

http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html

Divine Mercy Chaplet Guide (A Powerful prayer):

https://play.google.com/store/apps/details?id=com.dmercyapp


Comments

Popular posts from this blog

Copy a single file using robocopy

Copy a single file using robocopy from a local folder to a shared folder on the network.
A simple rule of thumb before any disaster strike, don't interchange the source and the destination.

If source and destination is mistakenly reverse, files might get overwritten. To avoid any loss of data do a test with a dummy file to ensure things work perfectly.
Robocopy [source][destination]   [file to be copied]
robocopy c:\local_c_folder  \\PC_network\shared_folder   file_to_be_copied_xx.txt
The command will be completed successfully provided the network access right has no issues.

Robocopy works quite good on large files. A simple copy or xcopy command will also work but the speed might vary.

Robocopy is free it can be accessed from command line. No need to install the resource kit tool if the operating system is Windows 7 or newer version.

Copy files with selected file extension using PowerShell and Robocopy:

$extension = ('.pdf', '.jpg', '.txt')
gci d:\WorkFolde…

WMIC get computer name

WMIC get computer model, manufacturer, computer name and  username.
WMIC is a command-line tool and that can generate information about computer model, its manufacturer, its username and other informations depending on the parameters provided.
Why would you need a command line tool if there’s a GUI to check?
If you have 20 or 100 computers, or even more. It’s quite a big task just checking the GUI to check the computer model and username.
If you have remote computers, you need to delegate someone in the remote office or location to check.
Or you can just write a batch file or script to automate the task.
Here’s the code below on how get computer model, manufacturer and the username.
Open an elevated command prompt and type:
wmic computersystem get "Model","Manufacturer", "Name", "UserName"
Just copy and paste the code above, the word “computersystem” does not need to be change to a computer name.
A sample output below will be generated if the co…