I'm new to creating vbs files. I've used VBA quite a lot so I thought it might be easy to start using them. I created this code in a module in a database to test the date modified on a two files, and if they differ then update the destination file. The idea is to test if there's a new copy of the frontend on the server and if so copy it over. It works fine in the database but if I copy it into notepad and save it as a vbs file it creates an error.
Option Explicit
Public Sub sOpenFile()
On Error GoTo ErrorHandler
Dim fso As FileSystemObject
Dim fsoFile As File, fsoFileDest As File
Dim dtmSource As Date
Dim dtmDestination As Date
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.GetFile("C:\My Documents\Test.txt")
Set fsoFileDest = fso.GetFile("C:\Documents and Settings\Rob\Desktop\Test.txt")
dtmSource = fsoFile.DateLastModified
dtmDestination = fsoFileDest.DateLastModified
If dtmSource > dtmDestination Then
fsoFile.Copy "C:\Documents and Settings\Rob\Desktop\Test.txt", True
End If
ExitProcedure:
Set fsoFile = Nothing
Set fso = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 53
fsoFile.Copy "C:\Documents and Settings\Rob\Desktop\Test.txt", True
Case Else
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation
End Select
Resume ExitProcedure
End Sub
Option Explicit
Public Sub sOpenFile()
On Error GoTo ErrorHandler
Dim fso As FileSystemObject
Dim fsoFile As File, fsoFileDest As File
Dim dtmSource As Date
Dim dtmDestination As Date
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.GetFile("C:\My Documents\Test.txt")
Set fsoFileDest = fso.GetFile("C:\Documents and Settings\Rob\Desktop\Test.txt")
dtmSource = fsoFile.DateLastModified
dtmDestination = fsoFileDest.DateLastModified
If dtmSource > dtmDestination Then
fsoFile.Copy "C:\Documents and Settings\Rob\Desktop\Test.txt", True
End If
ExitProcedure:
Set fsoFile = Nothing
Set fso = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 53
fsoFile.Copy "C:\Documents and Settings\Rob\Desktop\Test.txt", True
Case Else
MsgBox Err.Number & ": " & Err.Description, vbOKOnly + vbExclamation
End Select
Resume ExitProcedure
End Sub