How to remove directories using wildcards by reading a file using batch

batchbatch-filewindows-command-prompt

Batch file

for /f "delims=" %%f in (7profiledeletelist.txt) do rd /s /q "%%f"
PAUSE
exit

7profiledeletelist.txt

C:\Users\1*
C:\Users\2*
C:\Users\3*
C:\Users\4*
C:\Users\5*
C:\Users\6*
C:\Users\7*
C:\Users\8*
C:\Users\9*
C:\Users\M*
C:\Users\T*

After reading the 7profiledeletelist.txt file, the bat file can not delete anything.

If I remove * and write the exact name of the directory it is working well. I want to delete all folders starting with M, T, 1 until 9 in the Users directory.

How does 7profiledeletelist.txt or the batch script need to change in order to make it work? Is there a mistaken code in the batch script?

Best Answer

According to this Q&A, rmdir does not accept wildcards. However, issuing the following command:

for /D %f in (1*) do rmdir %f /s /q

removes all folders starting with 1.

According to this Q&A it is possible to read a file line by line using batch.

rmdir_regex.bat

@echo off

for /f "tokens=*" %%a in (7profiledeletelist.txt) do (
  for /D %%f in (%%a) do rmdir %%f /s /q
)