1.

Solve : Referencing a specific file creation date in a .bat or .vbs?

Answer»

I am attempting to create a script that checks if a file's creation date is x days old. If the file is x days old, then I need to execute the file and delete the script. It is unnecessary to delete the file itself.

I have seen many solutions online for creating a batch/vbs that checks ALL files in a folder and deletes the ones that are x days old. This is not my intended function...hopefully someone here has a suggestion for me...

I would prefer to do this in a batch file...but I have heard that vbs would be a better solution for scripts attempting to call date variables. I'm open to either method, but I need to USE a switch when running the exe.vbs version:
1. check x days old: datediff function, combine with datecreated property
2. execute file: .run method or .exec method
3. delete file: delete or deletefile method

batch version:
1. check x days old: use julian-gregorian math, combine with dir/tc
2. execute file: call,start method or just type the filename
3. delete file: del

if you post your batch/vbs script, along with explanation on what you want.Code: [Select]Public Function DeleteOldFiles(5 DaysOld As LONG, FileSpec As _
String,Optional ComparisonDate As Variant) As Boolean

'PURPOSE: DELETES ALL FILES THAT ARE DaysOld Older than
'ComparisonDate, which defaults to now

'RETURNS: TRUE, if succesful
'False otherwise (e.g., user passes non-date argument
'deletion fails because file is in use,
'file doesn't exist, etc.)

'will not delete readonly, hidden or system files

Dim sFileSpec As String
Dim dCompDate As Date
Dim sFileName As String
Dim sFileSplit() As String
Dim iCtr As Integer, iCount As Integer
Dim sDir As String

sFileSpec = FileSpec

If IsMissing(ComparisonDate) Then
dCompDate = Now
ElseIf Not IsDate(ComparisonDate) Then
'client passed wrong type
DeleteOldFiles = False
Exit Function
Else
dCompDate = CDate(Format(ComparisonDate, "mm/dd/yyyy"))
End If

sFileName = Dir(sFileSpec)

If sFileName = "" Then
'returns false is file doesn't exist
DeleteOldFiles = False
Exit Function
End If

Do

If sFileName = "" Then Exit Do

If InStr(sFileSpec, "\") > 0 Then
sFileSplit = Split(sFileSpec, "\")
iCount = UBound(sFileSplit) - 1
For iCtr = 0 To iCount
sDir = sDir & sFileSplit(iCtr) & "\"
Next

sFileName = sDir & sFileName
End If

On Error GoTo errhandler:
If DateDiff("d", FileDateTime(sFileName), dCompDate) _
>= DaysOld Then

Kill sFileName

End If

sFileName = Dir
sDir = ""
Loop

DeleteOldFiles = True

Exit Function

errhandler:
DeleteOldFiles = False
Exit Function
End Function

That's what I'm working from in VBS...but I have no idea where to go from here. The code is from freevbcode (http://www.freevbcode.com/ShowCode.Asp?ID=77). I've been tinkering with VB script for the first time this week and I've accomplished some basic tasks like changing where I want my My Documents targeted but this time comparison is proving difficult for me...even while referencing msdn frequently.

Here's what I want blah.vbs to do exactly:
If "C:\myfile.exe" is 5 days old, run "C:\myfile.exe /silent" and delete "C:\blah.vbs"

I'll end up changing the paths so I can put blah.vbs in the startup folder...but I think that gets my idea across.After spending some time with your suggestions...I've come up with this. What do you think?

Code: [Select]dim filesys, filetxt, f, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set filesys = CreateObject("Scripting.FileSystemObject")
Set f = filesys.GetFile("C:\Basic_admin.exe")

If DateDiff("d",f.DateCreated,Now) >= 5 Then
WshShell.Exec "C:\Basic_admin.exe /install"
filesys.DeleteFile "C:\documents and settings\administrator\start menu\programs\startup\freezeit.vbs"
Else
wscript.echo "Your computer will remain thawed for " & 5 - DateDiff("d",f.DateCreated,Now) & " more days. Be sure to have all devices installed before then!"
End if

QUOTE from: MrQuincy on April 02, 2009, 04:49:46 PM

After spending some time with your suggestions...I've come up with this. What do you think?

Code: [Select]dim filesys, filetxt, f, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set filesys = CreateObject("Scripting.FileSystemObject")
Set f = filesys.GetFile("C:\Basic_admin.exe")

If DateDiff("d",f.DateCreated,Now) >= 5 Then
WshShell.Exec "C:\Basic_admin.exe /install"
filesys.DeleteFile "C:\documents and settings\administrator\start menu\programs\startup\freezeit.vbs"
Else
wscript.echo "Your computer will remain thawed for " & 5 - DateDiff("d",f.DateCreated,Now) & " more days. Be sure to have all devices installed before then!"
End if

Lol, you just answer your own question. this is excellent, since you write it yourself, it's easier for you to maintain the code. nice script Assuming you want the script to delete itself,

This would need editing if the name changed:

Code: [Select]filesys.DeleteFile "C:\documents and settings\administrator\start menu\programs\startup\freezeit.vbs"
This would not:

Code: [Select]filesys.DeleteFile "Wscript.ScriptFullName"Quote from: Dias de verano on April 03, 2009, 05:17:24 AM
Assuming you want the script to delete itself,

This would need editing if the name changed:

Code: [Select]filesys.DeleteFile "C:\documents and settings\administrator\start menu\programs\startup\freezeit.vbs"
This would not:

Code: [Select]filesys.DeleteFile "Wscript.ScriptFullName"


HA HA! it would so!

Code: [Select]filesys.DeleteFile Wscript.ScriptFullName
to remove the quotes at least Quote from: BC_Programmer on April 03, 2009, 01:26:09 PM

HA HA! it would so!

Code: [Select]filesys.DeleteFile Wscript.ScriptFullName
to remove the quotes at least

Indeed. Good catch.


Discussion

No Comment Found