1.

Solve : How to delete folders with common characters name?

Answer»

I want to delete a lot of sub-folders with files. Folders named with some common characters e.g. /foldera/200406, /folderb/200407, /folderc/200408 ...... many.
How to write a batch file to delete all of them without change directory. Is it require any for each loop ?
Pls help.Batch code has two wildcard characters that might be helpful here: * and ?

The asterisk represents a whole word or group of characters. The question mark represents a single character.

In your case something like:

del \folder?\2004??

might work.

Be very careful. Using wildcards can have unintended and disastrous results especially since files DELETED thru the command prompt do not make a side trip to the recycle bin.

8-)
Hi, thx for your ADVICE, I tried most cases as shown below.

C:\del c:\stats\?\2004??
The filename, directory name, or volume label syntax is incorrect.

C:\del c:\stats\*\2004??
The filename, directory name, or volume label syntax is incorrect.

C:\del c:\stats\*\2004*
The filename, directory name, or volume label syntax is incorrect.

but all are not working.

:-?Oops! To delete folders use:

rd \folder?\2004??

Depending on your OS, the RD command has switches which may be safer than wildcards.

Im using winxp dos command.

rd is not working also.

c:\rd c:\stats\?\2004?? /s
c:\stats\?\2004??, Are you sure (Y/N)? y
The filename, directory name, or volume label syntax is incorrect.

and same as
c:\rd c:\stats\*\2004* /s

even
c:\rd c:\stats\foldera\2004?? /s
c:\stats\foldera\2004??, Are you sure (Y/N)? y
The filename, directory name, or volume label syntax is incorrect.

It seems wildcard cannot be used.

:-?Microsoft may have overlooked the no wildcards with RD in their documentation. But there are other ways to do this. Create a set of directories matching the pattern, iterate thru the set, and pick them off one by one:

Code: [Select]@echo off
for /f "tokens=* delims=" %%i in ('dir /b /a:d c:\stats\?\2004??') do (
rd /s "%%i"
)

The above code is for use in a batch file. Using * as a wildcard may produce unexpected results.

8-)

Note: You may want to replace rd /s with echo for the first run. This will enable you to see what will be deleted before you actually delete anything.Hi,

It is not working either with same reply "The filename, directory name ..... is incorrect."

But it works only when the path is specified with exact location and run after CD to that directory, otherwise file is not found.

@echo off
for /f "tokens=* delims=" %%i in ('dir /b /a:d c:\stats\foldera\2004??') do (
rd /s "%%i"
)

Seemed that c:\stats\?\2004?? is not work. I have so many folderX. Kindly pls help solving this.

:-?


I guess wildcards can only be used at the end of a string. Best thing to do is keep plugging until you get it right but when things get to a point where you're nesting FOR statments, it may be TIME to rethink your whole approach.

In any case, this may work:

Code: [Select]@echo off
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d c:\stats\?') do (
for /f "tokens=* delims=" %%x in ('dir /s /b /a:d %%i\2004??') do (
echo "%%x"
)
)

There may be some "File Not Found" messages. Disregard, as these are folders that don't fit the naming convention.

Note: I left the echo in; change to rd /s when happy with the results.

8-)Not work. I copy batch file everywhere but just only reply the system prompt no matter where I started.

c:\
or
c:\stats\
or
c:\stats\foldera

No synthx or ERROR message ??




Actually I was able to get this code to work quite well in my test environment:

Code: [Select]@echo off
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d [highlight]c:\stats\?[/highlight]') do (
for /f "tokens=* delims=" %%x in ('dir /s /b /a:d %%i[highlight]\2004??[/highlight]') do (
echo "%%x"
)
)

The pattern (mask) may be incorrect on the DIR commands (highlighted for your VIEWING pleasure). I'll leave you to straighten that out.

Note: The question mark (wildcard) on the first DIR command refers to one and only one character. The double question marks on the second DIR command refer to two and only two characters.

Good luck. 8-)

Special note: I would not run this from a directory that might be affected by the logic. Removing the directory you're logged into may create problems.Hi Sidewinder,

It is the DIR problem. Smooth running when I change c:\stats\? to c:\stats\*.
I have deleted over 1300 folders with more than 70000 files in just 2 mins.

THANKYOU so much for your GREAT HELP.



Discussion

No Comment Found