Reputation: 91
I'm trying to write a script in batch file that do the following:
My code is:
@echo off
for /f "delims=" %%a in ('dir /b /s /a:d "C:\project\" ^|findstr /e /i "\Mdata"') do (
@echo "%%~a"
setlocal
set target=%%a
set days=-30
for /f "usebackq delims=" %%G in (
`forfiles /p "%target%" /c "cmd /c if /i @isdir == true echo @path" /d %days% 2^>nul`
) do rd /s /q "%%~G"
endlocal & exit /b
)
pause
The first task to locate all "Mdata" folders working well. but the delete dosen't work.
Upvotes: 1
Views: 164
Reputation: 6558
This should take care of it.
I simplified your script a bit to remove the inner FOR
loop since you can just perform your deletion from withing the FORFILES
command.
@echo off
SETLOCAL
set days=-30
for /f "delims=" %%a in ('dir /b /s /a:d "C:\project\" ^|findstr /e /i "\Mdata"') do (
echo "%%~a"
forfiles /p "%%~a" /d %days% /c "cmd /c if /i @isdir==true echo @path & rd /s /q @path"
)
ENDLOCAL
pause
The reason your script wasn't working is because you did not have delayed expansion enabled, so the target
and days
would not be usable for your inner FOR
loop. As it stands though, you don't need this functionality enabled for what you are trying to accomplish.
Upvotes: 1