Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bat file for remove file that is older that an hour 1

Status
Not open for further replies.

pho01

Programmer
Mar 17, 2003
218
US
is anybody out there familiar enough with a bat script that helps me delete files that are older than an hr?

dir C:\Server\logs > C:\temp\logs_remove.txt
for /F "eol=; tokens=2" %%i in ('date /T') do set TODAY_DATE=%%i
for /F "eol=; tokens=1" %%i in ('time /T') do set TODAY_TIME=%%i
for /F "eol=; tokens=1" %%i in (C:\temp\mfdir_remove.txt) do ...

do command only allows me with one command statement, so wonder if someone would be kind and knowledgeable to help me accomplish this task.

Thanks much!
 
You can accomplish this much easier with a vbs script.

look up filesystemobject

That will allow you to directly access the file creation or file modification dates, do your comparison, and then decide about deleting the file or not.
 
Here is a sample script that shows how to delete files and folders based on age.

Code:
'==========================================================================
'
' VBScript Source File -- 
'
' NAME: CleanBadMail.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]	
' Copywrite (c) 2003 All rights reserved
' DATE  : 09/10/2003
'
' COMMENT: 
'
' This script will list all filtered and quarantined SPAM mail, check that 
' the files are more than 30 days old and then delete them.
' This file is to be scheduled to run each day.
'=====================================

Path1 = "E:\Program Files\Exchsrvr\Mailroot\vsi 1\BadMail"
Path2 = "E:\Program Files\Trend\SMCF\Quarantine"


Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
   	If DateDiff("d", oFile.DateCreated,Now) > 30 Then
    	oFile.Delete True
    End If
  Next


Set oFolder = fso.GetFolder(Path2)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
   	If DateDiff("d", oSubFolder.DateCreated,Now) > 30 Then
		fso.DeleteFolder(oSubFolder)
	End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
i'm not familiar with vbscript. how is it execute and how do i view the builtin functions?

is there a recommended url for tutorials? can't find good urls on the web that i'm looking for, more like vbscript embedded for html stuffs.

thanks much
 
So Mark,
It deleted files that are older than a day, how about files that are older than an hour? What function should i use instead then?

Thanks,
Phuong
 
Do the datediff using the following

FileHour = DatePart("H",oFile.DateCreated)
ThisHour = DatePart("H",Now)

I hope you find this post helpful.

Regards,

Mark
 
Cool. thanks Mark. One quick question: what command to use to move files to a different folder instead of deleting?

like move all files from path1 to path1 if files are older than 1 hr.

I greatly appreciate your help on this. You're great!
 
use the object.Move method. You first need to bind to the file as shown below.
As shown below, the file will be moved to C:\Projects\Test.

Code:
dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")
set demofile = filesys.GetFile("c:\somefile.txt")
demofile.Move ("c:\projects\test\")

I hope you find this post helpful.

Regards,

Mark
 
just a thought...
How do I add a try catch block around the actual move since one or two of the files could be in use and we don’t want the script crashing every time because of one file?
 
Use On Error Resume Next.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top