Batch script to delete file older than 30 day [Solved/Closed]

sd6340 1 Posts Thursday February 26, 2009Registration date February 27, 2009 Last seen - Feb 27, 2009 at 02:32 PM - Latest reply:  Dude
- Dec 13, 2017 at 09:10 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!
See more 

30 replies

+48
Helpful
14
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.
The @file returns the file name but not the full path, so deletion won't work recursively (as the author appropriately stated "at the root directory"). The @path command passes Delete the full file path so it can delete files in subdirectories. Here's what I use:

:: Recursively delete log files older than 180 days from PATH
forfiles /M *.log /P "PATH" /S /D -180 /C "cmd /c del /F /Q @path"
Worked Great Thank you all!!
Works well. thanks.
+21
Helpful
2
Deletion:

find ./ -type f -mtime +30 -exec rm -- {} \;

Copy:

rsync -avxSH <source> <destination>
Please, Exercise caution with the above command!!!

Finding all the old files starting up from your root folder and excluding the old files Is really NOT a good idea!!!

Substitute ./ with the path of your choice and be smart enough not to put a system or config folder in the way of this command.
Not that you shouldn't be cautious, but './' is the current directory and below, not root '/'. Note the addition of the '.'
+16
Helpful
how to delete a file older than 7 days ???
+5
Helpful
Please share the answer of this script with me, thanks.
+4
Helpful
is there a way to delete all PDF files in a certain directory?
+3
Helpful
1
he was asking for a "batch" script (on windows , .bat) , and not a *nix "bash" script :)
i'm looking for the same thing as well :)
+3
Helpful
The following windows script will move files older than a given date from C:\folder1 to C:\folder2. By older, I am assuming that they are not modified after a certain date. The date is passed in the format yyyymmdd . When files are moved to folder2, they are automatically deleted from folder1.

# Script movefiles.txt
# Input argument - date
var str date

# Get a list of files not modified since $date.
var str list ; lf -n "*" "C:\folder1" ( ($ftype == "f") AND ($fmtime < $date) ) > $list
# Move files one by one.
while ($list <> "")
do
    var str file ; lex "1" $list > $file
    system move $file "C:\folder2"
done


The script is in biterscripting. Download biterscripting from http://www.biterscripting.com . Save the script as C:\Scripts\movefiles.txt. Start biterscripting. Call the script as

script movefiles.txt date("20090613")


The above will move files that have not been modified since June 13, 2009 (30 days before today).

Sen
SenHu 15 Posts Friday May 22, 2009Registration date February 2, 2010 Last seen - Jan 14, 2010 at 10:07 AM
+3
Helpful
Email Id removed for security said:

 
> We generate about 1000 reports per hour on our webserver and 
> run a nightly script to remove these files. Unfortunately, that is not often enough 
> and need to have a script that we can run every hour 
> that removes old files (older than say 1 hr).  

> Can someone steer me to a solution that uses the system time, 
> and removes files from our "reports" directory that are older? Thx in Advance



This script will do exactly what you need. I will assume that these report files are in folder "C:/hourly reports". I am adding comments so you can follow the logic.


# Script HourlyDelete.txt 
# Go to reports folder. 
cd "C:/hourly reports"  
# Collect a list of files that were CREATED more than an hour ago. 
var str list, file 
lf -r -n "*" "." ( $fctime < addtime(diff("-10000")) ) > $list 
# Delete each file in $list one by one. 
while ($list <> "") 
do 
    lex "1" $list > $file 
    echo -e "DEBUG: Deleting file " $file 
    system del ("\""+$file+"\"") 
done





Script is in biterscripting. Test it first on a test folder. Change "C:/hourly reports" in the script to correct path. Double quotes are required around paths. To test, save the script in file C:/Scripts/HourlyDelete.txt, then enter the following command in biterscripting.



script "C:/Scripts/HourlyDelete.txt"





Once you know it's working, schedule the following command in task scheduler to run hourly.

"C:/biterScripting/biterScripting.exe" "C:/Scripts/HourlyDelete.txt"
+2
Helpful
We want to move ( not DELET ) log files after every 5 minutes form one location to another.

But also we do not want to move the log file which is currently active.

Means we want to move the logfiles which are 5 minutes older than System Date.

Can we get the Windows Script for the same, which we can use in the windows scheduler.
SenHu 15 Posts Friday May 22, 2009Registration date February 2, 2010 Last seen - Feb 2, 2010 at 10:29 AM
+2
Helpful
We want to move ( not DELET ) log files after every 5 minutes form one location to another. 

But also we do not want to move the log file which is currently active. 

Means we want to move the logfiles which are 5 minutes older than System Date. 

Can we get the Windows Script for the same, which we can use in the windows scheduler.




Hi Ami:

When you say "5 minutes older than system date", I assume you refere to file modification time ?

Here is the windows script in biterscripting.


# Script MoveLog5min.txt
var str source, destination, pattern
# Create a time string that represents "5 minutes earlier" (subtract 05 mins, 00 seconds from current time).
var str time
set $time = addtime(diff("-0500"))
# Collect a list of files matching $pattern in folder $source whose modification time is earlier than $time.
var str list, file
lf -n $pattern $source ( ($ftype=="f") AND ($fmtime < $time) ) > $list
# Go thru files one by one.
while ($list <> "")
do
    # Get the next file.
    lex "1" $list > $file
    # Move file to $destination.
    system -s ("move /Y \""+$file+"\" \""+$destination+"\"")
done




Save the script in file "C:/Scripts/MoveLog5min.txt", start biterscripting, enter the following command.


script "C:/Scripts/MoveLog5min.txt" source("C:/Log Directory") destination("C:/Moved Log Directory") pattern("*.log")


This will move all files matching name "*.log" which have not been modified in the last 5 minutes from source folder "C:/Log Directory" to destination folder "C:/Moved Log Directory" .


Test it first. When all working, schedule the following command in task scheduler.

"C:/biterscripting/biterscripting.exe" "C:/Scripts/MoveLog5min.txt" source("C:/Log Directory") destination("C:/Moved Log Directory") pattern("*.log")


(Just replace the command word 'script' to the path of biterscripting executable.)


Do let me know how this works out for you.
rebeccazoly 1 Posts Tuesday June 4, 2013Registration date June 5, 2013 Last seen - Jun 4, 2013 at 03:29 PM
0
Helpful
I have download Long Path Tool and it is the best solution of your problem.
-32
Helpful
1
No need any script to remove a file. Just select the file which you want to remove and press 'Del' or Delete button in the keyboard. It will automatically remove the file.

Regards,
veerasami.
The script will automatically do it, versus having to manually do it. If you have a ton of files, you don't want to be sitting there deleting one by one...