tags:

views:

1528

answers:

3

Exact Duplicates:

I want write a batch file to delete folders, subfolders and temporary files on time basis.

The conditions are like this:

  • I need to delete the file from folder one hour before to current running time.
  • I want to get the current time from system and delete the one hour before files
  • The file format is like this: <file name> <date>
  • The time format is dd-mm-yyyy-hh-mm or dd-mm-yy-hh-mm
+2  A: 

Batch can be surprisingly powerful but I don't think the kind of date manipulation you want will be practical.

You can use FOR to split the date into elements and then use SET /A to do the subtraction, but then you're going to need a huge number of IF and GOTO statements to handle cases like subtracting an hour from half past midnight on the 1st of January.

I think you'd be better off investigating VBS or Powershell.

Dave Webb
A: 

See this answer and adapt accordingly. Shouldn't be hard.

Joey
Two things. 1. Yikes - there are all those IFs and GOTOs I was worried about. 2. Looking at the original revision of this question and that one it's pretty clear that the same person asked both so why didn't just read your answers there?
Dave Webb
1. That's what I do best :D, 2. There are some "unknown (google)" people around here that don't tend to read answers. Don't know why they ask in the first place, then. Maybe finding their question a few days later again is nigh impossible on SO. Or they're just trolling :)
Joey
A: 

You may want to try this script to delete files from a folder that are older than 1 hour.

# Script Delete1Hour.txt
var str folder, list, file
cd $folder
lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) ) > $list
while ($list <> "")
do
    lex "1" $list > $file
    system del ("\""+$file+"\"")
done

This is biterscripting. The important command is

lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) )

Which tells biterscripting - give me all entries in folder $folder with names that match the patten "*" whose file type is "f" (flat file, not directory), and whose file modification time ($fmtime) is earlier than 1 hour. See the documentation for the lf command at http://www.biterscripting.com/helppages/lf.html .

Save the script in file C:/Scripts/Delete1Hour.txt, run it as

script  "C:/Scripts/Delete1Hour.txt" folder("C:/testfolder")

It will delete all files from C:/testfolder that are older than 1 hour.

P M