Create basic filelist using XML-logging and vbs script
Hi,
Many thanks for Martin Prikryl for excellent winscp.
What I was missing was a simple file list. For example the Windows command line FTP-client's (ftp.exe) ls command creates simple filelist with the filenames only, like this:
file1.ext
file2.ext
file3.ext
However, winscp.com's ls adds additional file data (date, size etc.).
When I'm scripting FTP/SFTP/FTPS filetransfers I like to check before the transfer whether the file exists, and therefore the simple filelist is useful.
Here's how to do simple filelist with winscp using XML-logging and a vbs script.
Use xml-logging with winscp:
winscp.com /log=transfer_log.xml /script=myscript.txt
The myscript.txt looks like this:
open sftp://myaccount:MyPa55word@myserver.example.com
ls
exit
The VBS script
' list_files_in_winscp_xml-log.vbs Dim strFilename, intReturncode Dim objArguments, objFileSystem, objXmldoc, objNodes Set objArguments = WScript.Arguments If (objArguments.Count = 1) Then strFilename = objArguments(0) Else WScript.Echo "XML-log file name missing." WScript.Quit(1) End If Set objFileSystem = CreateObject("Scripting.FileSystemObject") If Not objFileSystem.FileExists(strFilename) Then WScript.Echo "XML-log file does not exist, quitting." WScript.Quit(2) End If Set objXmldoc = CreateObject("MSXML2.DOMDocument") objXmldoc.Load(strFilename) intReturncode = objXmldoc.setProperty("SelectionNamespaces", "xmlns:w='http://winscp.net/schema/session/1.0'") Set objNodes = objXmldoc.selectNodes("//w:file") For Each Node in objNodes If Node.selectSingleNode("w:type/@value").value = "-" Then WScript.Echo Node.selectSingleNode("w:filename/@value").value End If Next
Running the VBS script and saving the result in a file
cscript //nologo list_files_in_winscp_xml-log.vbs transfer_log.xml > filelist.txt
The filelist.txt now contains something like this:
file1.ext
file2.ext
file3.ext
Using the filelist.txt
Directly in the command prompt you can use filelist.txt to check if files with same name as in FTP/SFTP/FTPS -server exist in your folder:
FOR /F %A in (filelist.txt) do If Exist "\\MyServer\MyFolder\%~nxA" Echo File %~nxA exists.
Here's how to do the same in a batch script:
Rem MyBatchscript.bat FOR /F %%A in (filelist.txt) do ( If Exist "\\MyServer\MyFolder\%%~nxA" ( Echo File %%~nxA exists. ) Else ( Echo File %%~nxA DOESN'T exists. ) )
Last edited by ciove on 2011-07-05 09:03; edited 1 time in total