Great post!
@Guest: Great post! Thank you! I had to do a few of my own tweaks, but this was exactly what I needed.
put
command, only moving new or updated files and setting the archive bit. (if anyone is interested, the two .ini file settings are NewerOnly=1
and ClearArchive=1
). This took way too long, too and was generally unreliable anyway.
set shell = WScript.CreateObject("WScript.Shell")
set exec = shell.Exec("winscp.com /log=c:\bin\transfer.xml")
' set up our session
exec.StdIn.Write("option batch continue" + vbCRLF & _
"option batch continue" + vbCRLF & _
"option confirm off" + vbCRLF & _
"option transfer binary" + vbCRLF & _
"option reconnecttime 300" + vbCRLF& _
"open myuser@myserver.xxx" + vbCRLF)
'set up a file system object that will traverse the directory we're checking
Set fso = CreateObject("Scripting.FileSystemObject")
set shell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Unnamed.Count >= 1 Then
If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then
strSourceDir = WScript.Arguments.Unnamed(0)
strTargetDir = WScript.Arguments.Unnamed(1)
Recurse fso.GetFolder(WScript.Arguments.Unnamed(0))
Else
WScript.Echo "Folder not found."
End If
Else
WScript.Echo "Please give folder name as argument 1."
End If
Sub Recurse(f)
For Each sf In f.SubFolders
Recurse sf
Next
For Each sf In f.Files
If sf.attributes And 32 Then 'archive attribute is set so lets upload that file
putfile = replace(sf, strSourceDir, strTargetDir, 1, -1, 1)
putfile = replace(putfile, "\", "/")
exec.StdIn.Write("put """ + sf + """ """ + putfile + """" + vbCRLF)
Wscript.Echo "put """ + sf + """ """ + putfile + """" + vbCRLF
End If
Next
End Sub
WScript.Echo "done looking..."
exec.StdIn.Write("exit" + vbCRLF) 'we've finished evaluating all the files. Our winscp session may still be going on, however...
sInput = exec.StdOut.ReadAll() 'this allows the session to complete. The script will pause here until it can "readall" the output
'lets look at the xml log file that winscp wrote
Set doc = CreateObject("MSXML2.DOMDocument")
doc.async = False
doc.load("c:\bin\transfer.xml")
' look for uploaded files
Set NodeList = doc.documentElement.selectNodes("//upload")
WScript.Echo("There are " & nodelist.length & " files")
'traverse all the uploaded files and look for those that were sucessfully uploaded so we can remove the archive bit
For i = 0 To NodeList.length - 1
filename = nodelist(i).selectSingleNode("filename/@value").value
If nodelist(i).selectSingleNode("result/@success").value="true" Then 'if the file was successfully transferred...
fso.getfile(filename).attributes = fso.getfile(filename).attributes -32 'remove the archive attribute
End If
Next