Home > Mobile >  How to include system variable in batch file inside FOR loop ? also escaping the ! and % sign inside
How to include system variable in batch file inside FOR loop ? also escaping the ! and % sign inside

Time:02-15

lets say i have a list file contains folder names that i want to delete periodically based on a list,

currently using this batch file which don't work as i expected :

setlocal enabledelayedexpansion
for /F "tokens=2* delims==" %%x in ('findstr/brc:"foldertodelete" garbagefolderlist.txt') do (
    set "foldertodelete=%%x"
    set foldertodelete=!foldertodelete:"=!
    set foldertodelete=!foldertodelete:%%=^!!"
    echo !foldertodelete!
    if exist !foldertodelete! (
        echo deleting !foldertodelete!
        rmdir /s /q !foldertodelete!>nul
        )
    )
endlocal

inside garbagefolderlist.txt :

foldertodelete="%programfiles%\blablabla"
fodlertodelete=%systemroot%\blablabla
foldertodelete="C:\Temp"
foldertodelete=D:\Temporary files\here

notes about the list file (garbagefolderlist.txt) :

1. folder names may contains double quotes or not, so i want to dynamically eliminate the double quotes inside batch file

2. folder names may be plain or using system variable or not like %systemroot%, etc

3. folder names may contains spaces

CodePudding user response:

If all you want to do is delete the folders listed in that text file you only need one single line of code. You need to use the CALL command to get the double variable expansion that you require. You don't even need delayed expansion at all.

for /F "tokens=2* delims==" %%x in ('findstr /bc:"foldertodelete" garbagefolderlist.txt') do call rmdir /s /q "%%~x" 2>nul

Here is the execution of your script on my system. I created a folder in Program Files and I also have a Temp folder on the C: drive. The line in your file with %systemroot% will not be chosen because you have a typo on that line. So the script will only attempt to process three lines from your input example.

I have added the echo to the code and removed the error dump to nul so that you can see all the output.

@echo off
for /F "tokens=2* delims==" %%x in ('findstr /bc:"foldertodelete" garbagefolderlist.txt') do (
    call echo %%~x
    call rmdir /s /q "%%~x"
)

And here is the output of that code.

C:\BatchFiles\SO\71120676>so.bat
C:\Program Files\blablabla
Access is denied.
C:\Temp
D:\Temporary files\here
The system cannot find the path specified.

So lets break down that output.

  1. You can see that the %programfiles% variable is expanded as it echo's the folder name correctly but the folder cannot be deleted because I am not running from an elevated cmd prompt as Administrator. So that folder cannot be deleted.
  2. The temp folder displays correctly and is deleted.
  3. The last directory does not exist on my system as I don't have a D: drive so the system reports that it cannot find the path. If standard error was still being redirected to the NUL device you would not see the error which is why I don't bother with checking if a folder exists before I delete it.
  • Related