User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Existing Scripts section within the Web Development category of DaniWeb, a massive community of 391,697 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,139 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Existing Scripts advertiser: Web Code Converter
Views: 31096 | Replies: 10
Reply
Join Date: Nov 2006
Posts: 7
Reputation: sliver_752 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sliver_752's Avatar
sliver_752 sliver_752 is offline Offline
Newbie Poster

Question recursive delete using a batch file on windows-XP

  #1  
Nov 13th, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: recursive delete using a batch file on windows-XP

  #2  
Nov 13th, 2006
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++.
Last edited by Ancient Dragon : Nov 13th, 2006 at 4:14 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: recursive delete using a batch file on windows-XP

  #3  
Nov 14th, 2006
There should be an option to delete an entire tree, so you wouldn't need to recursively delete it.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: recursive delete using a batch file on windows-XP

  #4  
Nov 14th, 2006
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:\
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Mar 2007
Posts: 1
Reputation: vanillabean is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vanillabean vanillabean is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #5  
Mar 8th, 2007
Originally Posted by sliver_752 View Post
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:
Last edited by vanillabean : Mar 8th, 2007 at 2:00 am.
Reply With Quote  
Join Date: Mar 2007
Posts: 1
Reputation: dgarcia182 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dgarcia182 dgarcia182 is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #6  
Mar 27th, 2007
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"
Reply With Quote  
Join Date: Apr 2007
Posts: 2
Reputation: doggfather is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doggfather doggfather is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #7  
Apr 16th, 2007
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.
Reply With Quote  
Join Date: Apr 2007
Posts: 2
Reputation: doggfather is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
doggfather doggfather is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #8  
Apr 16th, 2007
Hrmm Actually. It does work. My bad. I needed single quotes when testing it outside the batch file.
Reply With Quote  
Join Date: May 2007
Posts: 1
Reputation: jdcravens is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jdcravens jdcravens is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #9  
May 5th, 2007
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.
Last edited by jdcravens : May 5th, 2007 at 5:23 pm.
Reply With Quote  
Join Date: Apr 2007
Posts: 1
Reputation: jpkeisala is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jpkeisala jpkeisala is offline Offline
Newbie Poster

Re: recursive delete using a batch file on windows-XP

  #10  
Apr 24th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Existing Scripts Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Existing Scripts Forum

All times are GMT -4. The time now is 2:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC