To determine how many files you've deleted, you can modify your batch script to count the files before deleting them. This involves adding a counter that increments for each file found that meets your criteria. Here's how you can adjust your script:
@echo off
setlocal enabledelayedexpansion
:: Define the directory path
set "directory=C:\Users\PESZQ\Desktop\test\tutaj"
:: Initialize file count
set "FILECOUNT=0"
:: Count the files older than 15 days
for /f "delims=" %%A in ('forfiles /p "%directory%" /s /d -15') do (
set /a FILECOUNT+=1
)
if !FILECOUNT! gtr 0 (
echo !FILECOUNT! files older than 15 days exist. Deleting them...
forfiles /p "%directory%" /s /d -15 /c "cmd /c del @file >nul"
echo !FILECOUNT! files have been deleted.
) else (
echo No files older than 15 days found.
)
pause
timeout /t 120
Explanation:
-
Enable Delayed Variable Expansion:
- At the beginning of the script,
setlocal enabledelayedexpansion is added. This allows the script to update and read variables within loops and conditional statements using the !VARIABLE! syntax.
-
Initialize the Counter Variable:
set "FILECOUNT=0" initializes a variable to keep track of the number of files.
-
Counting Files:
-
Conditional Execution Based on File Count:
-
Deleting Files:
-
Display the Result:
Additional Notes:
By implementing these changes, your script will count the number of files that are about to be deleted and display that count before and after the deletion process.
Let me know if you have any questions or need further assistance!