1,105,371 Community Members

recursive delete using a batch file on windows-XP

Member Avatar
sliver_752
Newbie Poster
7 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Ancient Dragon
Achieved Level 70
27,632 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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

Member Avatar
Ancient Dragon
Achieved Level 70
27,632 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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:\

Member Avatar
vanillabean
Newbie Poster
1 post since Mar 2007
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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:

Member Avatar
dgarcia182
Newbie Poster
1 post since Mar 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"
doggfather
Newbie Poster
2 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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.

doggfather
Newbie Poster
2 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
jpkeisala
Newbie Poster
1 post since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
kanyan
Newbie Poster
1 post since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jdcravens
Newbie Poster
1 post since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
pixelgrafitti
Newbie Poster
1 post since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: