Hello All,
Below is a script that saves a file with time/time stamp also delete files older than 8 hours... The problem is the code is erroring out before compeleting.
The error messeage I'm getting is as follows:
Line: 58
Char: 1
Error: Type mismatch: 'cdate'
Code: 800A000D
Source: Microsoft VBScript runtime error
CODE:
Option Explicit
dim iFileDeleted
Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim strCMD : strCMD = "cmd /c stsadm.exe -o backup -url -filename C:\SGD_" & Convert_FullDate(now) & ".bak"
WScript.Echo strCMD
Dim errReturn
errReturn = objShell.Run(strCMD, 0, True)
If errReturn = 0 Then
MsgBox "Backup reported no errors"
iFileDeleted = DeleteFile("C:\")
If iFileDeleted=0 then
Msgbox "No File Deleted"
else
Msgbox iFileDeleted & " File(s) Deleted"
end if
Else
MsgBox "Backup exited with error code: " & errReturn
End If
'Convert date to string format : "MM_DD_YYYY_HHMM"
Function Convert_FullDate(dtm_Date)
'Get Day e.g. Tue
dim sDay
dim sdate
dim stime
sDay = left(FormatDateTime(dtm_Date,1) ,3)
'get date 08_25_2009
sDate= replace(FormatDateTime(dtm_Date,2),"/","_") 'mm/dd/yyyy
sTime= replace(FormatDateTime(dtm_Date,4),":","_")
Convert_FullDate=sdate & "_" & sTime
End Function
'Convert date to string : format : "ddd MM_dd_YYYY"
Function Convert_FullDateTime(dtm_Date)
'Get Day e.g. Tue
dim sDay
dim sdate
sDay = left(FormatDateTime(dtm_Date,1) ,3)
'get date 08_25_2009
sDate= replace(FormatDateTime(dtm_Date,2),"/","_") 'mm/dd/yyyy
Convert_FullDateTime=sDay & " " & sdate
End Function
'Calculate File Age by Filename : filename format = 'XXX_mm_DD_YYYY_HHMM.XXX"
function AgeDiff(dtm_date,fname)
'getTime
dim sDateTime
dim fTime
dim stime
dim sDate
dim fdate
'NSG_08_25_2009_0330.bak
sdate = left(right(fname,len(fname)-4),15) ' get DateTime from file name, without extension (.bak)
stime = right(sdate,4) 'hh:mm
fdate= cdate(replace(left(sdate,10),"_","/") & " " & left(stime,2) & ":" & right(stime,2))
agediff= datediff("h",fdate,dtm_date)
end function
'Check files in specified folder, delete if , age > 8 hours
Function DeleteFile(sCurPath)
Dim Fso
Dim Directory
Dim Modified
Dim Files
dim iCount
'sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
icount = 0
Set Fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(sCurPath) Then
Set Directory = Fso.GetFolder(sCurPath)
Set Files = Directory.Files
For Each Modified in Files
msgbox modified.name & " " & Agediff(now,modified.name)
If Agediff(now,modified.name) > 8 Then
Modified.Delete
iCount = iCount + 1
End if
Next
else
msgbox "Path not found !"
End if
DeleteFile = iCount
End Function
Please advice.
THanks.
Below is a script that saves a file with time/time stamp also delete files older than 8 hours... The problem is the code is erroring out before compeleting.
The error messeage I'm getting is as follows:
Line: 58
Char: 1
Error: Type mismatch: 'cdate'
Code: 800A000D
Source: Microsoft VBScript runtime error
CODE:
Option Explicit
dim iFileDeleted
Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim strCMD : strCMD = "cmd /c stsadm.exe -o backup -url -filename C:\SGD_" & Convert_FullDate(now) & ".bak"
WScript.Echo strCMD
Dim errReturn
errReturn = objShell.Run(strCMD, 0, True)
If errReturn = 0 Then
MsgBox "Backup reported no errors"
iFileDeleted = DeleteFile("C:\")
If iFileDeleted=0 then
Msgbox "No File Deleted"
else
Msgbox iFileDeleted & " File(s) Deleted"
end if
Else
MsgBox "Backup exited with error code: " & errReturn
End If
'Convert date to string format : "MM_DD_YYYY_HHMM"
Function Convert_FullDate(dtm_Date)
'Get Day e.g. Tue
dim sDay
dim sdate
dim stime
sDay = left(FormatDateTime(dtm_Date,1) ,3)
'get date 08_25_2009
sDate= replace(FormatDateTime(dtm_Date,2),"/","_") 'mm/dd/yyyy
sTime= replace(FormatDateTime(dtm_Date,4),":","_")
Convert_FullDate=sdate & "_" & sTime
End Function
'Convert date to string : format : "ddd MM_dd_YYYY"
Function Convert_FullDateTime(dtm_Date)
'Get Day e.g. Tue
dim sDay
dim sdate
sDay = left(FormatDateTime(dtm_Date,1) ,3)
'get date 08_25_2009
sDate= replace(FormatDateTime(dtm_Date,2),"/","_") 'mm/dd/yyyy
Convert_FullDateTime=sDay & " " & sdate
End Function
'Calculate File Age by Filename : filename format = 'XXX_mm_DD_YYYY_HHMM.XXX"
function AgeDiff(dtm_date,fname)
'getTime
dim sDateTime
dim fTime
dim stime
dim sDate
dim fdate
'NSG_08_25_2009_0330.bak
sdate = left(right(fname,len(fname)-4),15) ' get DateTime from file name, without extension (.bak)
stime = right(sdate,4) 'hh:mm
fdate= cdate(replace(left(sdate,10),"_","/") & " " & left(stime,2) & ":" & right(stime,2))
agediff= datediff("h",fdate,dtm_date)
end function
'Check files in specified folder, delete if , age > 8 hours
Function DeleteFile(sCurPath)
Dim Fso
Dim Directory
Dim Modified
Dim Files
dim iCount
'sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
icount = 0
Set Fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(sCurPath) Then
Set Directory = Fso.GetFolder(sCurPath)
Set Files = Directory.Files
For Each Modified in Files
msgbox modified.name & " " & Agediff(now,modified.name)
If Agediff(now,modified.name) > 8 Then
Modified.Delete
iCount = iCount + 1
End if
Next
else
msgbox "Path not found !"
End if
DeleteFile = iCount
End Function
Please advice.
THanks.