- 1 year ago
I want to delete a stale files which is >365 days along with print the list of files are deleted in cmd via batchfile(.bat)
To delete all files that are older than 365 days and print the list of deleted files in the Windows command prompt using a batch file, you can use the forfiles
command and pipe the output to the del
command and the echo
command.
Here's an example batch file:
batch@echo off
setlocal
set "target_directory=C:\path\to\directory"
forfiles /p "%target_directory%" /s /m *.* /d -365 /c "cmd /c echo @path & del @path /q"
endlocal
This batch file sets the target_directory
variable to the directory you want to clean up. It then uses the forfiles
command to find all files in the directory and its subdirectories (/s
) that are older than 365 days (/d -365
) and run a command for each file.
The command (/c
) it runs is a cmd
command that first echoes the path of the file using echo @path
, then deletes the file using del @path /q
. The /q
option is used to suppress the confirmation prompt.
The output of the echo
command will be the list of deleted files.
Note that this batch file will permanently delete files. Make sure to test the batch file on a test directory and not on important data.