0

I need your help for writing a little batch script in order to delete specific named folders inside a path.

Let's image we have multiple folders called "pippo" inside the path tree "C:\Users\myUser\Desktop\StartFolder"

How can I write a script that browses all the folder tree of that path and delete all fsubolders called "pippo" ?

Thanks!


I found this command in other site for /d /r "%d" %d in (_svn) do @if exist "%d" rmdir "%d"

So I tried to adapt it to my target, but it doesn't work. for /d /r "C:\Users\myUser\Desktop\StartFolder" "pippo" in (_svn) do @if exist "pippo" rmdir "pippo"

4
  • 1
    The way SO works is that you show the code you have been trying, describe what it should be doing, and describe how it is failing. SO is not a free script writing service. Commented May 15, 2018 at 14:55
  • Ok, sorry, i'll provide what I tried until now. Commented May 15, 2018 at 14:56
  • What happens to any directories or files within the pippo directory tree? Commented May 15, 2018 at 15:19
  • It has to be removed. But answer below solved my issue. Commented May 15, 2018 at 15:32

1 Answer 1

1

It's unclear to me what the _svn is meant for in your tries.
( or is _svn your real pippo ?)

for /r "C:\Users\myUser\Desktop\StartFolder" /d %%A in (pippo ) do if exist "%%~fA" echo rmdir "%%~fA" 

If the output looks OK, remove the echo in front of rmdir

Sample output on my test tree:

> for /r "q:\Test\2018" /D %A in (05) do @if exist "%~fA" @echo rd "%~fA" rd "q:\Test\2018\05" rd "q:\Test\2018\04\05" rd "q:\Test\2018\05\05" 
Sign up to request clarification or add additional context in comments.

5 Comments

I thought the /D option required a wildcard!
@Compo I did a test and included it in my answer.
Ok, this works! I did not understand that "(_svn)" was the name of the name of the folder I wanna delete.
@compo it simply appends to the path from /r if present or not, so an if exist is necessary.
As long as no wildcard is used, for [/R] does not access the file system and just returns the item(s) after in, so /D is not needed but does not harm either; usually I do add /D in case directories are to be processed just for indication...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.