The script should display subdir names in summary, instead i am getting "Files deleted…
- User
The script should display subdir names in summary, instead i am getting "Files deleted inside DIR_TO_PROCESS:=: 8".
[SCRIPT]
@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" set "TOTAL_FILES_DELETED=0"
for %%D in (%directories%) do ( echo Processing directory %%D ... set "ROOT_DIR=%%D"
code:: Get the root directory name for %%R in ("!ROOT_DIR!") do set "ROOT_NAME=%%~nxR" :: Get all directories including subdirectories for /f "delims=" %%G in ('dir /AD /S /B "!ROOT_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%\=!" :: Handle the case where the directory is the root if "!RELATIVE_DIR!"=="" set "RELATIVE_DIR=." :: Increment summary count and store the line set /a SUMMARY_COUNT+=1 set /a TOTAL_FILES_DELETED+=!FILECOUNT! set "SUMMARY_LINE[!SUMMARY_COUNT!]=Files deleted inside !RELATIVE_DIR!: !FILECOUNT!" ) ) - Assistant
The issue in your script is due to the way you're trying to extract the relative directory path. Specifically, the line:
batchset "RELATIVE_DIR=!DIR_TO_PROCESS:%ROOT_DIR%\=!"is causing problems because variable expansion inside another variable's substitution can be tricky, especially with delayed expansion and special characters like backslashes.
To fix the problem, you can simply extract the directory name (the last part of the path) instead of trying to compute the relative path. You can do this by using the
FORloop with the~nxmodifiers, which give you the name and extension of a file or directory (for directories, it's just the name):Replace the problematic line with:
batch:: Get the directory name for %%R in ("!DIR_TO_PROCESS!") do set "RELATIVE_DIR=%%~nxR"This will correctly set to the name of the directory you're processing (like "1", "2", "3", etc.), and your summary will display the correct directory names.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.