2

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?

0

1 Answer 1

3

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
)
2
  • Hi utrecht Can we add an exception folders (not to be deleted) to this code? For ex: Folder Name: is 235812 and 35284?
    – serdar
    Oct 21, 2014 at 13:17
  • @serdar This means that the remove command needs to be encapsulated by an e.g. if not statement which is compared by e.g. 2*==235812. This is hard to implement using batch to my opinion. However, PowerShell has more features than batch. Perhaps you could try to implement this using PowerShell and post the attempts in a new question if it does not work.
    – 030
    Oct 21, 2014 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .