I want to write batch file which will loop through all directories containing backup directory and remove files older than X days within it. On computer which I want run my script there's no "forfile" command. There's no PowerShell, so CMD or VBScripts seems to be only way of accomplishing this task.
Currently I have problem with "set" statement - it seems that when I'm calling %checkpath% I didn't receive expected folder.
rem we will memorize current directory
pushd %cd%
set folder="C:\Documents and Settings\myname\Desktop"
cd %folder%
rem loop only folders with five chars within their names (unfortunately on less also
for /D %%g in (?????) DO (
set checkpath="%cd%\%%g\backup"
if exist %checkpath% (
for %%a in ('%%g\backup\*.*') do (
set FileDate=%%~ta
set FileDate=%%FileDate:~0,10%%
rem here I want to compare file modification data with current date
)
)
popd
pause
You need to use delayed expansion to read a variable that you have set inside a
for
loop.Try this instead
Replacing the
%
's with!
's on variables you have created will signal it to use delayed expansion instead.Bali's answer has a slight mistake. The second
set filedate
is incorrect, otherwise his is fine, but may not work if you do not have delayed expansion enabled. I fixed his mistake and showed you how to ensure delayed expansion is enabled. I have also made some other changes:But, you don't need the "setlocal" or delayed expansion if you write it like this: