Topic "VB Script to Silently Synchronize ftp site to local dir."

Author Message
esmith

Guest


I wrote this script to be called by task scheduler to synchronize a FTP site to a local directory, with a size criteria and to delete any changes to local directory. Also, This script will first check to see if Winscp.exe is running, and close it and restart the sync if the scheduled task runs into the next time it runs.It also calls a batch file that uses the /script switch with the .exe to run silently.

Here is the vbs script.

Code:

strComputer = "."
strProcessToKill = "winscp.exe"

SET objWMIService = GETOBJECT("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" _
   & strComputer & "\root\cimv2")

SET colProcess = objWMIService.ExecQuery _
   ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

sub shell(cmd)
' Run a command as if you were running from the command line
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub


count = 0
FOR EACH objProcess in colProcess
   objProcess.Terminate()
   count = count + 1
   
Next


shell "C:\admin\sync.bat"


Here is the bat file

Code:

@echo off
cd C:\program files\winscp\
start /b winscp.exe /script=sync1.txt
exit


If you guys see anything i can do differently please let me know.
esmith
[View user's profile]

Joined: 2012-11-20
Posts: 2
Location: United States
I have added some Functionality to this script. It can now do the following functions

-Check if required directories exist, and if not, creates them.
-Checks specific folder for log entries, and if it is over 7 days old, deletes them
-Makes the batch file from the script(no longer looks for sync1.txt)
-general improvements.

If you guys see anything that I can do better just let me know.


Code:

Dim Filesys
strComputer = "."
strProcessToKill = "winscp.exe"
FileName ="Blank Documents"
LogFolder = "C:\admin\BDSync\logs"

'Function To Get User Desktop Path
Function DesktopPath()
Dim WSHShell
Set WSHShell= CreateObject("WScript.Shell")
DesktopPath=WSHShell.SpecialFolders("Desktop")
Set WSHShell= Nothing
End Function

'Function To Get User Document Path
Function DocumentPath()
Dim WSHShell
Set WSHShell= CreateObject("WScript.Shell")
DocumentPath=WSHShell.SpecialFolders("MyDocuments")
Set WSHShell= Nothing
End Function

'Variable for User Document Folder
BDFolder= DocumentPath & + "\" + FileName

'Varible to check exsistance (and create) folders
SET Filesys=CreateObject("Scripting.FileSystemObject")
SET oLogFolder= Filesys.GetFolder(LogFolder)
SET oLogFolderCollection= oLogFolder.files

'Checks to see if "C:\admin\BDSync\logs" Exists, and if not, creates directory
If Not Filesys.FolderExists(LogFolder)Then
Filesys.CreateFolder(LogFolder)

End IF

'Number of days to keep log files
DaysOld= 7

'Deletes any log files older than the value in the "DaysOld" varible
For Each ofile in oLogFolderCollection
   
   If ofile.DateLastModified < (Date() - DaysOld) Then
   ofile.Delete(True)
   End IF
Next

'Checks to see if "C:\Users\USERNAME\Documents\Blank Documents" exsists
If Not Filesys.FolderExists(BDFolder)Then
Filesys.CreateFolder(BDFolder)

End IF

'Checks to see if a shortcut for the Blank Documents folder exists, and if it doesn't, it creates it
ShortcutName= DesktopPath & + "\" + FileName + ".lnk"
If Not Filesys.FileExists(ShortcutName)Then
SET shortcut = CreateObject("WScript.Shell").CreateShortcut(CreateObject("WScript.Shell").SpecialFolders("Desktop") & + "\" + FileName + ".lnk")
   shortcut.Description = "My shortcut"
   shortcut.TargetPath = BDFolder
   shortcut.Arguments = "/Arguments:Shortcut"
   shortcut.Save
End IF

'Sets "shell" to run a shell command
sub shell(cmd)
' Run a command as if you were running from the command line
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub

'Creates Sync.bat, which is called at the end of the script.(This is what passes the FTP parameters to the WinSCP Program)
SET objFSO = CreateObject("Scripting.FileSystemObject")

'Open write stream
SET outFile = objFSO.CreateTextFile("C:\admin\BDSync\sync.bat", True)

'Write to Batch File
outFile.WriteLine "@echo off"
outFile.WriteLine "cd %programfiles%\winscp\"
outFile.WriteLine "start /b winscp.exe /xmllog=""C:\admin\BDSync\logs\!M-!D-!Y@!T.xml"" /xmlgroups /command ""open ftp://username:password@ftp.somesite.com"" ""synchronize local -delete -criteria=size" & + " " + """""" + BDFolder + """""" + " " + "/"
outFile.WriteLine "exit"

'Close Write Stream
outFile.Close

'Allows script to access Running Processes"
SET objWMIService = GETOBJECT("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" _
   & strComputer & "\root\cimv2")

'set variable to Winscp Process Instance
SET colProcess = objWMIService.ExecQuery _
   ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

'Checks to see if an instance of WinSCP.exe is running, and if it is, it closes it. This will loop to close multiple processes
count = 0
FOR EACH objProcess in colProcess
   objProcess.Terminate()
   count = count + 1
   
Next

'Runs the batch file from a slient shell command
shell "C:\admin\BDsync\sync.bat"




Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License