I was going through some old files and thought I would post this one as a good example of using VBScript.
I had a request to automate the upload of a file to an FTP site. This example dates back to 2006 so I know there are better ways to do this now, but it is a good example of the flexibility of VB Scripting.
‘ VBScript source code
Dim Fso : Set Fso = CreateObject(“Scripting.FileSystemObject”)
Dim WshNetwork : Set WshNetwork = CreateObject(“Wscript.Network”)
Dim WshShell : Set WshShell = CreateObject(“WScript.Shell”)
Const LogFile = “C:\VBScript\FTP.log”
‘Declares the log file.
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Dim strFileName : strFileName = “C:\VBScript\holdings.csv”
‘Checks to see if the file is ready.
If not Fso.FileExists(strFileName) then
‘File is not ready exit out and wait
Set WshShell = WScript.CreateObject(“WScript.Shell”)
RunString = “%comspec% /c echo ************************ “ & WeekDayName(WeekDay(Now), True) & ” “ & Now & ” FILE NOT READY ************************ “
Return = WshShell.Run(RunString & ” >> ” & LogFile & ” 2>&1″, 0, TRUE)
WScript.quit
Else
RunCmd
Sub RunCmd()
Set WshShell = WScript.CreateObject(“WScript.Shell”)
RunString = “%comspec% /c echo ********************************************** “
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c echo “ & WeekDayName(WeekDay(Now), True) & ” ” & Now & “, “& strFileName & ” is available – sending via FTP”
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c echo ********************************************** “
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
‘FTP the file to client
RunString = “%comspec% /c %WINDIR%\System32\ftp.exe -i -n -s:C:\VBScript\instructions.txt “
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c echo ********************************************** “
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c echo FINISHED: “ & WeekDayName(WeekDay(Now), True) & ” “ & Now
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
RunString = “%comspec% /c echo ********************************************** “
Return = WshShell.Run(RunString & ” >> “ & LogFile & ” 2>&1″, 0, TRUE)
Set WshShell = Nothing
End Sub
End
Basically this script will check to see if a file exists and if it does it will send the file via FTP to a remote location and log all the output to a log file. This is a good simple script to quickly and repeatedly perform a task and log it.
Filed under: VB Script, Windows Server Tagged: scripting, VB Script
