Hi all

I am trying to write a batch file which would recursively delete folders with a specific name inside a tree. i am just not able to do that. Please help me.

thanks for your help in advance

Recommended Answers

All 13 Replies

batch files are generally pretty simple-minded things, not at all as powerful as *nix shell scripts. For more complicated tasks you would be better off writing a program in some other language, such as C or C++.

Member Avatar for iamthwee

There should be an option to delete an entire tree, so you wouldn't need to recursively delete it.

yes there is that option, but the way I understand it is that he only wants to delete selected files/folders, not all of them. Use "rd /s" command to remove all the files and directories in the current tree. warning: do not do this from root directory such as c:\

Hi all

I am trying to write a batch file which would recursively delete folders with a specific name inside a tree. i am just not able to do that. Please help me.

thanks for your help in advance

This will do the trick:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G

If you're thinking WTF?:
DIR /B /AD /S *.svn*
lists all files that are named ".svn"
/B makes the output "bare" with nothing but the file name
/AD only lists directories
/S recurses subdirectories to include their contents if they match the listing criteria


RMDIR /S /Q [path/name]
deletes the directory [path/dir] and all of it's children

FOR /F processes each item (in this case directories) in the set IN ('[command]') by executing the DO [command]

%%G is a parameter, which in this example is a directory name
"tokens=*" says that all the characters output in a line (i.e. by the dir command) are assigned to the parameter %%G

See the Microsoft Command Line Reference for more on FOR:

commented: Nice +3

Thanks for the script, it works great! Some of my directories would not delete due to having spaces in the directory names, for instance C:\out\IQSv3\Portal\Web References\Service\.svn. Adding quotes around the %%G fixed this problem.

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"

I am having issues with this command... I get the following error when trying to use this:

D:\Backup to Disk # 1>FOR /F "tokens=*" %%G IN ('DIR /b /ad /s "d:\Backup to Disk # 1\img*"') DO RMDIR /S /Q "%%G"

%%G was unexpected at this time.

It is referencing the first instance of that. I do not know why... any ideas? I am trying this on server 2003 to remove a backup to disk instance once it is backed up to a tape incase anyone wondered.

Hrmm Actually. It does work. My bad. I needed single quotes when testing it outside the batch file.

I haven't tried batch file but if you want to do this just once you can use normal windows search. Just search .svn and check option search hidden files in advanced options. Then just delete all .SVn's from search results.

hi i am new this forum, unable to find where to post my question.
i need a batch file ,should delete oldfiles except last 15 days, means i should retain only last 15 days folder. the folder name is based on date,ex- today folder will be 20070428.pl help.thanks in advane.

thanks
kanyan

Doggfather - Im getting the same error - when applying single quotes from command line. Does anyone know what Im doing wrong...Ive tried:

1. FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G

2. FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q '%%G'

3. FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"

4. FOR /F "tokens=*" '%%G' IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"

5. FOR /F "tokens=*" "%%G" IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"

6. FOR /F "tokens=*" '%%G' IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G

7. FOR /F "tokens=*" "%%G" IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G

No matter what I do i get:
%%G was unexpected at this time.

Doggfather - Im getting the same error - when applying single quotes from command line. Does anyone know what Im doing wrong...

No matter what I do i get:
%%G was unexpected at this time.

Yes I know exactly what your doing wrong. This will not work if entered directly at the command prompt because the double % syntax is only for running the command from a batch file. From the command line you only need a single %G for it to work properly. Hope that help somebody out there who was looking for the answer to this one.

Hello,

Create a text file and copy the below command line:

Echo batch file delete folder

@RD /S /Q "D:\testfolder"

Save like a DeleteFolder.bat. Save anywhere except D:\testfolder.

Change "D:\testfolder" with your folder path and done.

More information on blog Batch file delete folder

Best regards

That doesn't do what the OP requested. Plus, why bother to create a batch file that contains only one line and does only one specific thing for a specific folder?

Also, please do not revive old threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.