Batch script to delete file older than 30 day

Solved/Closed
sd6340
Posts
1
Registration date
Thursday February 26, 2009
Status
Member
Last seen
February 27, 2009
- Updated on Jul 19, 2017 at 06:03 AM
 eie - Apr 18, 2019 at 12:20 AM
Hello,

i would like to create the batch script which could copy the 30 days older files from one folder to another. And also deletes the 30 days old files from the source folder after copying to the destination folder.


Any help would be appreciated!

12 replies

Try the following command to automatically delete files at the root of a directory. This command can be saved in a .bat file and scheduled with scheduled tasks.

forfiles /p "PATH" /s /d -30 /c "cmd /c del @file : date >= 30 days >NUL"

Change "PATH" with the path where you want the files to be deleted. If there are no spaces in the path you dont need the "-symbol.
61
didn't work
0
Works perfectly
0
Works out of command line and a batch for all fixed drives, but it cannot cope with UNC (\\<machine>\share) folders. If you map the UNC chare to a drive, then the batch files will work fine. But, Task Scheduler rejects both. No solution for a server user.
0
I'm running WinXP and I get an error
'forfiles' is not recognized as an internal or external command.
I've tried to run this at the command line and in a batch file.
Ideas?
0
On a Win 2000 Server I had to download forfiles from
https://petri.com/download_free_reskit_tools 
Using this version, I also had to remove the spaces between the command options and their value. After that I specified my directory, changed the time search value, and the command worked great
0