Kioskea
Recherche
Ask a question Report

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

sd6340 1Posts Thursday February 26, 2009Registration date February 27, 2009Last seen - Latest answer on Jan 9, 2014 10:38AM
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!
Read more 
Answer
+34
moins plus
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.

Envithyx- Apr 29, 2013 04:28PM
Um, I made two mistakes above... while the concept was right. Specifically I quoted your null when it's NUL and in the case of the command in question, the inline comment doesn't actually generate an error but would have redirected the output to a file called 30, which redirecting to nul avoids. In some cases, depending on the command, the : comment will actually produce undesirable effects or errors.
Justfish- Aug 13, 2013 03:22PM
worked perfectly, thank you for the information.
LYF4CE- Oct 10, 2013 01:28PM
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"
Jaime- Dec 23, 2013 08:34PM
Worked Great Thank you all!!
Prasad- Jan 9, 2014 10:38AM
Works well. thanks.
Answer
+19
moins plus
Deletion:

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

Copy:

rsync -avxSH <source> <destination>

D.eco- Sep 25, 2009 12:48PM
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.
null- Mar 10, 2010 12:32AM
Not that you shouldn't be cautious, but './' is the current directory and below, not root '/'. Note the addition of the '.'
Answer
+5
moins plus
Please share the answer of this script with me, thanks.

Answer
+4
moins plus
is there a way to delete all PDF files in a certain directory?

Answer
+4
moins plus
To delete all PDF files in a certain directory (say C:/Directory1), use this script.

# Script DeletePDF.txt
var str list ; lf -n "*.PDF" "C:/Directory1" > $list
while ($list <> "")
do
    var str file ; lex "1" $list > $file
    system -r del /FQ $file
done


Save the script as C:/Scripts/DeletePDF.txt, start biterscripting ( http://www.biterscripting.com ), enter the following command

script "C:/Scripts/DeletePDF.txt"

Answer
+2
moins plus
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 :)

monai- Mar 6, 2013 06:52AM
onnu pode
Answer
+2
moins plus
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

Answer
+2
moins plus
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"

Answer
+2
moins plus
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.

Answer
+2
moins plus
how to delete a file older than 7 days ???

Answer
+1
moins plus
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.

Answer
+0
moins plus
I have download Long Path Tool and it is the best solution of your problem.

Answer
-30
moins plus
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.

Anonomyus- Apr 7, 2010 12:45PM
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...
This document entitled « batch script to delete file older than 30 day » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.

Not a member yet?

sign-up, it takes less than a minute and it's free!

Members get more answers than anonymous users.

Being a member gives you detailed monitoring of your requests.

Being a member gives you additional options.