Reputation: 5573
So what I'm trying to do is go into every folder in the following directory
"C:\Documents and Settings\"
and for every folder in it, regardless of the name, check if this path exists
"C:\Documents and Settings\*\Local Settings\Application Data\CSMRpt\"
if it exists then delete all txt files inside that director, if the path doesn't exists then do nothing and move on to the next folder inside "C:\Documents and Settings\"
This is what I came up with so far:
set PATH = "\Local Settings\Application Data\CSMRpt\"
set FILETYPE = "*.txt"
for /d %%g in ("C:\Documents and Settings\*") do if exist %%g%PATH% goto pathexists
:pathexists
del %%g%PATH%%FILETYPE%
Upvotes: 1
Views: 5165
Reputation: 4193
A couple things wrong here, you can't have spaces around the =
in the set command, using goto
wouldn't have passed the variable (you could however use call instead and pass it as a argument) you don't need quotes around every variable, %PATH%
although you can reset it, you shouldn't for something like this as it is a environment variable.
Corrected code:
set THEPATH=\Local Settings\Application Data\CSMRpt\
set FILETYPE=*.txt
for /d %%g in ("C:\Documents and Settings\*") do if exist "%%g%THEPATH%." del "%%g%THEPATH%%FILETYPE%"
If you really didn't want the for
loop to be one line you could do this as well
set THEPATH=\Local Settings\Application Data\CSMRpt\
set FILETYPE=*.txt
for /d %%g in ("C:\Documents and Settings\*") do if exist "%%g%THEPATH%." call :deltxtfiles "%%~g"
exit /B
:deltxtfiles
del "%~1%THEPATH%%FILETYPE%"
goto:eof
Upvotes: 2
Reputation: 24476
Try this.
@echo off
setlocal
set cwd=%CD%
set p=Local Settings\Application Data\CSMRpt
cd /d "c:\Documents and Settings\"
for /d %%I in (*) do (
if exist "%%I\%p%\" (
pushd "%%I\%p%\"
del /q *.txt
popd
)
)
:: (change back to original directory)
cd /d "%cwd%"
Upvotes: 3