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

How to save a file with date/time stamp automatically 1

Status
Not open for further replies.

sotando

Programmer
Aug 26, 2009
21
US
The code shown below suppose to create the file name and the date/time stamp but it is giving me errors.

Error Message:

Script: C:\Documents and Settings\tim\desktop\Final_Rev.vbs
Line: 5
Char: 17
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)


Script:

Option Explicit
Dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
Dim strCMD : strCMD = "stsadm -o backup -url -filename C:\data_" & Convert_Date(Date) & "_0805.bak"
WScript.Echo strCMD
Dim errReturn : errReturn = objSHell.Run(strCMD, 0, True)
If errReturn = 0 Then
MsgBox "Backup reported no errors"
Else
MsgBox "Backup exited with error code: " & errReturn
End If
Function Convert_Date(dtm_Date)
Convert_Date = Right("0" & Month(dtm_Date), 2) &_
Right("0" & Day(dtm_Date), 2) &_
Year(dtm_Date)
End Function

Please advice.
Thanks.
 
Use the Format$() function to get your date in the format you need.
Format$(Date,"mmddyyyy")
 
ettienne,
The Format$() function is not available in vbscript
 
It does however have the (admittedly less versatile) FormatDateTime function. The equivalent of ettienne's function woud be:

Replace(FormatDateTime(Date),"/","")
 

replace(date, "/", "-") will yield "8-27-2009" as the file name. Append the extension and you're good to go.

-Geates

I just realized the pretty much what strongm wrote :)
 
And yet another alternative is to use the Convert_Date function that sotando is already using!! ;-)

However, the error seems to be with the "objSHell.Run(strCMD, 0, True)" command, probably because stsadm cannot be found.

Does the below line work from a command line ??
stsadm -o backup -url -filename C:\data_0805.bak
 
>I just realized the pretty much what strongm wrote :)

To be honest my 'solution' was really just to illustrate the existence of the FormatDateTime function. But it isn't really needed in this scenario.
 
Consideration:
I've never heard of Convert_Date in vbs and I find no reference to it. What is it? Does the return value contain spaces? If so, you'll have to embed quotes (" or chr(34)).

-Geates
 
I've never heard of Convert_Date in vbs and I find no reference to it
Look at the original post! And it doesn't insert spaces...
 
Geates, the Convert_Date function he made and included in his code snippet.
 
If strongm's suggestion does not work, I would try the line below before the objSHell.Run statement:
Code:
objShell.CurrentDirectory = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN"
Change the path to match your setup.
 
That's a good suggestion. The error could be cause by something as trivial as current directory.

Instead of objShell.Run, what about objShell.Exec? Granted, there is no window control but will it return something other than a stream?

-Geates
 
>The error could be cause by something as trivial as current directory

Shouldn't be the issue if, as the OP says, it works OK direct from the commandline
 
Thanks strongm changing my code to this:
strCMD = "cmd /c stsadm.exe -o backup -url -filename C:\data_" & Convert_Date(Date) & "_0805.bak"

Worked!! Thanks ALL for your response!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top