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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to figure out Type mismatch: 'cdate' error?

Status
Not open for further replies.

sotando

Programmer
Aug 26, 2009
21
US
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.
 
I would add some error trapping as shown below, so when the error occurs, you can get some insight as to what string is being converted.

Code:
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
[s]fdate= cdate(replace(left(sdate,10),"_","/") & " " & left(stime,2) & ":" & right(stime,2))[/s]
[COLOR=blue]sDateAndTime = replace(left(sdate,10),"_","/") & " " & left(stime,2) & ":" & right(stime,2)
on error resume next
fdate= cdate(sDateAndTime)
If err.number <> 0 Then
   wscript.echo "Got Error# " & err.number & ": " & err.description
   wscript.echo "fname = " & fname & "  sDateAndTime = " & sDateAndTime
End If
on error goto 0
[/color]
agediff= datediff("h",fdate,dtm_date)
end function
 
I thiunk you may be overcomplicating things. Whilst I understand that you want a human readable time stamp included in the filename you don't really need to be translating backwards and forwards from it to do date calculations on the files. Files have a number of date attributes that the FileSystemObject can access directly that return Dates that can be compared directly via DateDiff
 
I agree strongm. You can use the properties of the File object (.DateLastModified, .DateLastAccessed or .DateCreated) and compare them using DateDiff. It's considerably less complex (at least from a sytanx point of view)

-Geates
 
Thanks folks for the responses!
Does anybody knows how to change my code to use the properties of the file object(.DateCreated) and compare them using the DateDiff? I’m assuming that the code will look for all files in that directory that has the prefix SGD_ and delete all files older than 8 hours… Please advice. Thanks.
 
I find this to be an excellent reference. I strongly recommend visiting.

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFiles = objFSO.GetFolder("C:\path\").Files

for each objFile in objFiles
if ((left(objFile.Name, "4") = "SGD_") && (datediff("h", now, objFile.DateLastModified) > 8)) then objFile.Delete, true
next

-Geates
 
Perhaps it should be easier to read

for each objFile in objFiles
strPrefix = left(objFile.Name, "4")
intHoursOld = datediff("h", now, objFile.DateLastModified)
if ((strPrefix = "SGD_") && (intHoursOld >= 8)) then objFile.Delete, true
next
 
Er ... '&&' isn't a logical operator in VBScript. You'll be wanting 'And' instead ...
 
This is why scripts are in a folder entitled, "Dan's Dubious Scripts" :)

-Geates
 
Thanks folks for all the responses! It works now, but I decided to change the directory to this: \\nas000172\E\IDS\SQL Backup\SharePoint and it giving me this error message: Backup exited with error code: -2....Is SQL a reserve word in vbs? How can I point the script to save the files in this directory: \\nas000172\E\IDS\SQL Backup\SharePoint? Please advice.

Thanks.
 
My guess would be the space in the path. Make sure to encapsulate the directory in quotes chr(34). If that doesn't help try mapping a network drive instead of using UNC.

-Geates
 
STill getting an errror, Am I encapsulating the directory correctlly?

& chr(34) & \\nas000172\E\IDS\SQL Backup\SharePoint & chr(34) &

Please advice.
 
[tt]& chr(34) & x & chr(34) &[/tt]
x is taken as variable. As a variable, it has special requirements on its naming convention.
[tt]& chr(34) & "x" & chr(34) &[/tt]
"x" is taken as a string.
 
I think I know what causing the script to fail, it the space between SQL and Backup in the file name. Does anyone know how I can save a file into a folder that has spacing?

I have tried everything that I can think of, please help.

I'm tring to save files to the below directory:
\\nas000172\E\IDS\SQL Backup\SharePoint

Thanks.
 
as I said and tsuji examplified, you need to be sure to encapsulate your path in quotes . This must be done during any file operation where a space containing path is involved.

strFileName = chr(34) & "\\nas000172\E\IDE\SQL Backup\SharePoint" & chr(34)
- or -
strFileName = """\\nas00172\E\IDE\SQL Backup\SharePoint"""

why three quotes (""")? Because a quote is an escape character for a quote.

"""\\server\share\file.vbs"" /a /b" results in:
"\\server\share\file.vbs" /a /b

I guess I'm not sure what problem you are faced with.

-Geates
 
Thanks folks, the following did the trick!

strFileName = ""\\nas00172\E\IDE\SQL Backup\SharePoint
 
For that single line of strFileName, there are three choices: one quote, two successive, three successive quotes at both ends. The only wrong choice is two successive quotes. Isn't it amazing?
 
And how about setting strFileName to one quote; it requires a rediculous 4 quotes!

strFileName = """"

-Geates
 
Your post is not read, like your last but one!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top