I am getting wrong output even after some files were deleted @echo off setlocal…
- User
I am getting wrong output even after some files were deleted @echo off setlocal EnableExtensions EnableDelayedExpansion
set "directories=C:\Users\PESZQ\Desktop\test\tutaj C:\Users\PESZQ\Desktop\test\tutaj2"
:: Initialize the summary count set "SUMMARY_COUNT=0"
for %%D in (%directories%) do ( echo Processing directory %%D ... set "ROOT_DIR=%%D" if not "!ROOT_DIR:-1!"=="" set "ROOT_DIR=!ROOT_DIR!"
:: Get the root directory name for %%R in ("!ROOT_DIR:0,-1!") do set "ROOT_NAME=%%~nxR" set "CURRENT_DIR=!ROOT_DIR!" :: Get all directories including subdirectories for /f "delims=" %%G in ('dir /AD /S /B "!CURRENT_DIR!" 2^>nul') do ( set "DIR_TO_PROCESS=%%G" :: Initialize FILECOUNT for current directory set "FILECOUNT=0" :: Count files older than 15 days in DIR_TO_PROCESS for /f "delims=" %%H in ('forfiles /p "!DIR_TO_PROCESS!" /m . /d -15 2^>nul ^| find /c /v ""') do ( set "FILECOUNT=%%H" ) if !FILECOUNT! gtr 0 ( echo !FILECOUNT! files in directory "!DIR_TO_PROCESS!" are older than 15 days. Deleting them... :: Delete the files forfiles /p "!DIR_TO_PROCESS!" /m . /d -15 /c "cmd /c del @path" >nul 2>nul echo Deleted !FILECOUNT! files in directory "!DIR_TO_PROCESS!". :: Get the relative path from the root directory set "RELATIVE_DIR=!DIR_TO_PROCESS:%ROOT_DIR%=!" if "!RELATIVE_DIR!"=="" set "RELATIVE_DIR=." set "RELATIVE_DIR=!ROOT_NAME!!RELATIVE_DIR!" :: Increment summary count and store the line set /a SUMMARY_COUNT+=1 set "SUMMARY_LINE[!SUMMARY_COUNT!]=!FILECOUNT! files deleted inside !RELATIVE_DIR!" ) ))
:: Display the summary echo. echo Summary of files deleted: if %SUMMARY_COUNT% gtr 0 ( for /L %%i in (1,1,%SUMMARY_COUNT%) do ( call echo !SUMMARY_LINE[%%i]! ) ) else ( echo No files were deleted. )
pause Processing directory C:\Users\PESZQ\Desktop\test\tutaj ... Processing directory C:\Users\PESZQ\Desktop\test\tutaj2 ...
Summary of files deleted: No files were deleted. Press any key to continue . . .
- Assistant
It appears that your script isn't correctly displaying the summary because of the way variable expansion works in batch files, especially within code blocks (parentheses). Let's dive into the details and provide a solution.
Understanding the Issue
In batch scripting, there are two types of variable expansion:
- Immediate Expansion: Using
%VARIABLE%, which replaces the variable at parse time. - Delayed Expansion: Using
!VARIABLE!, which replaces the variable at execution time.
When you use variables inside code blocks (like
forloops orifstatements enclosed in parentheses), immediate expansion (%VARIABLE%) may not work as expected because the entire block is parsed before execution, and variables may not have the updated values during parsing.In your script, the
SUMMARY_COUNTvariable is incremented inside a code block, but you're trying to use it outside that code block without delayed expansion:if %SUMMARY_COUNT% gtr 0 ( for /L %%i in (1,1,%SUMMARY_COUNT%) do ( call echo !SUMMARY_LINE[%%i]! ) ) else ( echo No files were deleted. )At parse time,
%SUMMARY_COUNT%is likely still0, so the conditionif 0 gtr 0is false, resulting in "No files were deleted."Solution
To fix this, you should use delayed expansion for
SUMMARY_COUNTin your condition and loop. This means replacing%SUMMARY_COUNT%with!SUMMARY_COUNT!where necessary. Here's how you can modify your script:echo. echo Summary of files deleted: if !SUMMARY_COUNT! gtr 0 ( for /L %%i in (1,1,!SUMMARY_COUNT!) do ( call echo !SUMMARY_LINE[%%i]! ) ) else ( echo No files were deleted. )With this change,
!SUMMARY_COUNT!is expanded at execution time, reflecting the updated value after your deletion operations.Revised Script
Here's your script with the necessary modifications:
@echo off setlocal EnableExtensions EnableDelayedExpansion set "directories=C:\Users\PESZQ\Desktop\test\tutaj C:\Users\PESZQ\Desktop\test\tutaj2" :: Initialize the summary count set "SUMMARY_COUNT=0" for %%D in (%directories%) do ( echo Processing directory %%D ... set "ROOT_DIR=%%D" if not "!ROOT_DIR:-1!"=="" set "ROOT_DIR=!ROOT_DIR!" :: Get the root directory name for %%R in ("!ROOT_DIR:0,-1!") do set "ROOT_NAME=%%~nxR" set "CURRENT_DIR=!ROOT_DIR!" :: Get all directories including subdirectories for /f "delims=" %%G in ('dir /AD /S /B "!CURRENT_DIR!" 2^>nul') do ( set "DIR_TO_PROCESS=%%G" :: Initialize FILECOUNT for current directory set "FILECOUNT=0" :: Count files older than 15 days in DIR_TO_PROCESS for /f "delims=" %%H in ('forfiles /p "!DIR_TO_PROCESS!" /m * /d -15 2^>nul ^| find /c /v ""') do ( set "FILECOUNT=%%H" ) if !FILECOUNT! gtr 0 ( echo !FILECOUNT! files in directory "!DIR_TO_PROCESS!" are older than 15 days. Deleting them... :: Delete the files forfiles /p "!DIR_TO_PROCESS!" /m * /d -15 /c "cmd /c del @path" >nul 2>nul echo Deleted !FILECOUNT! files in directory "!DIR_TO_PROCESS!". :: Get the relative path from the root directory set "RELATIVE_DIR=!DIR_TO_PROCESS:%ROOT_DIR%=!" if "!RELATIVE_DIR!"=="" set "RELATIVE_DIR=." set "RELATIVE_DIR=!ROOT_NAME!!RELATIVE_DIR!" :: Increment summary count and store the line set /a SUMMARY_COUNT+=1 set "SUMMARY_LINE[!SUMMARY_COUNT!]=!FILECOUNT! files deleted inside !RELATIVE_DIR!" ) ) ) :: Display the summary echo. echo Summary of files deleted: if !SUMMARY_COUNT! gtr 0 ( for /L %%i in (1,1,!SUMMARY_COUNT!) do ( call echo !SUMMARY_LINE[%%i]! ) ) else ( echo No files were deleted. ) pauseAdditional Notes
- Pattern Matching with
forfiles: You used/m .withforfiles, but the correct way to match any file is/m *. Using/m .might not match any files. I've changed/m .to/m *in the revised script. - Variable Expansion in the Deletion Command: If you need to use variables within the
forfilescommand's/coption, you might need to adjust the syntax accordingly. - Testing the Script: Before running the script on important directories, test it on a safe directory to ensure it works as expected.
Conclusion
By modifying the script to use delayed expansion for
SUMMARY_COUNT, you ensure that the variable reflects its updated value when you check it at the end of your script. This should fix the issue and correctly display the summary of deleted files. - Immediate Expansion: Using