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.

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • Where are those two folder paths located in relation to your batch file? – Squashman Oct 06 '16 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["** – Compo Oct 06 '16 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 – Bagzli Oct 06 '16 at 15:27
  • @Compo yeah that has nothing to do with it, I already tested that. – Bagzli Oct 06 '16 at 15:28
  • Seems like it would be easier to just use the `RMDIR` command and then recreate the directory. – Squashman Oct 06 '16 at 15:38
  • @Compo, even this works: `set ,,, ;; a=1` sets variable `a` to `1`! – aschipfl Oct 06 '16 at 17:32
  • 2
    Possible duplicate of [Deleting Folder Contents but not the folder](http://stackoverflow.com/questions/38767550/deleting-folder-contents-but-not-the-folder) – aschipfl Oct 06 '16 at 17:41
  • You do not need to loop through each location recursively; simply delete all immediate files by `del *.*`, then loop through all immediate sub-directories and remove them with `rmdir`. The easiest way is for sure to deletethe entire root directory and recreate it, but I would actually not do that, because you would lose some information like attributes, creation timestamp, owner, permissions,... – aschipfl Oct 06 '16 at 17:43

3 Answers3

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
Squashman
  • 13,649
  • 5
  • 27
  • 36
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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 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. – Bagzli Oct 06 '16 at 15:35
  • Did you use a full path as I have, or are you still trying to use a supposed relative path? – Compo Oct 06 '16 at 15:38
  • Relative path still – Bagzli Oct 06 '16 at 15:40
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
soja
  • 1,587
  • 8
  • 8
  • I need to maintain my folder structure and only delete the contents within them. – Bagzli Oct 06 '16 at 15:46
  • 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. – soja Oct 06 '16 at 16:08
  • 2
    I would chose a different delimiter as a semi-colon and comma can be used in file and folder names. – Squashman Oct 06 '16 at 16:17