0

I am trying to loop through all the files and folder of specified directories and then delete them. I have very no previous experience with batch scripts and I have done quite a bit of online research, however I can't figure out why the following doesn't work:

@echo off CLS mode con: cols=160 lines=60 echo. ::**************************************************************** Set Location[0]=\lib\Debug Set Location[1]=\temp\ ::**************************************************************** :BEGIN echo start for /F "tokens=2 delims==" %%s in ('set Location[') do ( for /r %%i in (%%s) do echo %%i ) echo end pause 

What ends up happening is it lists all the files in the current directory of the batch script.

How do I make this script loop through all the directories specified in the Location variable and then delete all files and folders it finds in there. Clean Slate.

8
  • Where are those two folder paths located in relation to your batch file? Commented Oct 6, 2016 at 15:15
  • First thing to do is to Get rid of all of the pointless whitespace between Set and Location replacing it with a single space. That way we're sure you haven't got variables begining "spacespacespacespacespacespaceLocation[" Commented Oct 6, 2016 at 15:17
  • @Squashman the location is relative to the location of the batch file. So if the batch is in C:\temp then the files are in C:\temp\lib\Debug and C:\temp\temp Commented Oct 6, 2016 at 15:27
  • @Compo yeah that has nothing to do with it, I already tested that. Commented Oct 6, 2016 at 15:28
  • 2
    Possible duplicate of Deleting Folder Contents but not the folder Commented Oct 6, 2016 at 17:41

3 Answers 3

1

What this essentially does is delete the base folder and recreates.

 @echo off Set Location[0]=lib\Debug Set Location[1]=temp for /F "tokens=2 delims==" %%H in ('set Location[') do ( rmdir /Q /S "%%H" md "%%H" ) 

So here is a listing of my directory structure before I run the batch file.

 C:\BatchFiles\SO\rmdir>dir /b /s C:\BatchFiles\SO\rmdir\cleanup.bat C:\BatchFiles\SO\rmdir\lib C:\BatchFiles\SO\rmdir\temp C:\BatchFiles\SO\rmdir\lib\Debug C:\BatchFiles\SO\rmdir\lib\donotdelete C:\BatchFiles\SO\rmdir\lib\donotdelete.txt C:\BatchFiles\SO\rmdir\lib\Debug\deleteme C:\BatchFiles\SO\rmdir\lib\Debug\deleteme.txt C:\BatchFiles\SO\rmdir\lib\Debug\deleteme\deleteme.txt C:\BatchFiles\SO\rmdir\lib\donotdelete\donotdelete.txt C:\BatchFiles\SO\rmdir\temp\deleteme C:\BatchFiles\SO\rmdir\temp\deleteme.txt C:\BatchFiles\SO\rmdir\temp\deleteme\deleteme.txt 

Now I run the batch file and do a directory listing again. You can see it deletes out everything from temp and debug but leaves everything else.

 C:\BatchFiles\SO\rmdir>cleanup.bat C:\BatchFiles\SO\rmdir>dir /b /s C:\BatchFiles\SO\rmdir\cleanup.bat C:\BatchFiles\SO\rmdir\lib C:\BatchFiles\SO\rmdir\temp C:\BatchFiles\SO\rmdir\lib\Debug C:\BatchFiles\SO\rmdir\lib\donotdelete C:\BatchFiles\SO\rmdir\lib\donotdelete.txt C:\BatchFiles\SO\rmdir\lib\donotdelete\donotdelete.txt 
Sign up to request clarification or add additional context in comments.

Comments

0

See if this helps you:

@Echo Off Set "Location[0]=C:\temp\lib\Debug" Set "Location[1]=C:\temp\temp" For /F "Tokens=1* Delims==" %%A In ('Set Location[') Do ( For /R "%%B" %%C In (*.*) Do Echo=%%C) pause 

3 Comments

I have a list of dll's inside Debug and none of their names got printed. I had no output at all, just press key to continue. I only tested with Location[0]. I also copied and pasted your script and made no changes to make sure I don't have typos.
Did you use a full path as I have, or are you still trying to use a supposed relative path?
Relative path still
0

If you want to hardcode your folders to delete, you can do it this way:

@echo off & setlocal set todelete="lib\debug" "temp" "something else" "and this\folder\here" call :dodelete %todelete% goto :eof :dodelete if "%~1"=="" goto :eof rd /s "%~dp0%~1" md "%~dp0%~1" shift goto :dodelete 

3 Comments

I need to maintain my folder structure and only delete the contents within them.
This deletes all the files and folders (like you said) of the locations specified in todelete. If you want to keep the base folder, you could add md "%dpn0\%%~a" after the rd line.
I would chose a different delimiter as a semi-colon and comma can be used in file and folder names.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.