Create basic filelist using XML-logging and vbs script

Advertisement

ciove
Joined:
Posts:
9

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

Reply with quote

Advertisement

DuckAlumni
Guest

SSIS Get File Listing Output

Just another view of the work done by [ciove] using SSIS Control Flow component [Execute Process Task].

In the [Execute Process Task] component in the Process section:
RequireFullFileName: True
Execute: C:\Program Files\WinSCP\WinSCP.com
Arguments: /log=[destination and .XML filename; ex. C:\transfer_log.xml] /script=[script location to call; ex. C:\sFTPList.txt]
Working Directory: <blank>
StandardInputVariable: <blank>
StandardOutputVariable: <blank>
StandardErrorVariable: <blank>
FailTaksIfReturnCodeIsNotSuccessValue: True
SuccessValue: 0
TimeOut: 0
WindowStyle: Hidden


Script Task built to open execute WinSCP to send list command saved file as C:\sFTPList.txt:
option batch on
option confirm off
open sftp://myaccount:MyPa55word@myserver.example.com -hostkey="ssh-dss 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
# Below listing only files that have a .txt file format
ls *.txt
# Disconnect
close
exit


This should give you a listing of all files in an xml format based on the directory listing you choose.

Reply with quote

appu
Guest

file list not available in .xml log file

after running the script file using a .bat file .xml log is created but i am not able to find the file list in the log(by opening the .xml lof file)

.bat file has below code :

"c:\Program Files\WinSCP\WinSCP.com" /log=C:\appu\apputest.xml /script=C:\appu\testscript.txt

. 2012-11-25 01:50:30.808 --------------------------------------------------------------------------
. 2012-11-25 01:50:30.808 Using SFTP protocol.
. 2012-11-25 01:50:30.808 Doing startup conversation with host.
> 2012-11-25 01:50:30.808 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2012-11-25 01:50:30.808 Type: SSH_FXP_VERSION, Size: 124, Number: -1
. 2012-11-25 01:50:30.808 SFTP version 3 negotiated.
. 2012-11-25 01:50:30.808 Unknown server extension posix-rename@openssh.com=1
. 2012-11-25 01:50:30.808 Unknown server extension statvfs@openssh.com=2
. 2012-11-25 01:50:30.824 Unknown server extension fstatvfs@openssh.com=2
. 2012-11-25 01:50:30.824 Unknown server extension hardlink@openssh.com=1
. 2012-11-25 01:50:30.824 We believe the server has signed timestamps bug
. 2012-11-25 01:50:30.824 We will use UTF-8 strings for status messages only
. 2012-11-25 01:50:30.824 Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
. 2012-11-25 01:50:30.824 Getting current directory name.
. 2012-11-25 01:50:30.824 Getting real path for '.'
> 2012-11-25 01:50:30.824 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2012-11-25 01:50:30.824 Type: SSH_FXP_NAME, Size: 49, Number: 16
. 2012-11-25 01:50:30.824 Real path is '/home/puliurve'
. 2012-11-25 01:50:30.824 Startup conversation with host finished.
. 2012-11-25 01:50:30.824 Listing directory "/c_app/BEXT/j2/archive/check/jap1".
> 2012-11-25 01:50:30.824 Type: SSH_FXP_OPENDIR, Size: 51, Number: 267
< 2012-11-25 01:50:30.824 Type: SSH_FXP_HANDLE, Size: 13, Number: 267
> 2012-11-25 01:50:30.824 Type: SSH_FXP_READDIR, Size: 13, Number: 524
< 2012-11-25 01:50:30.824 Type: SSH_FXP_NAME, Size: 12747, Number: 524
> 2012-11-25 01:50:30.824 Type: SSH_FXP_READDIR, Size: 13, Number: 780
< 2012-11-25 01:50:30.824 Type: SSH_FXP_STATUS, Size: 28, Number: 780
< 2012-11-25 01:50:30.824 Status/error code: 1
> 2012-11-25 01:50:30.840 Type: SSH_FXP_CLOSE, Size: 13, Number: 1028
. 2012-11-25 01:50:30.840 Closing connection.
. 2012-11-25 01:50:30.840 Sending special code: 12
. 2012-11-25 01:50:30.840 Sent EOF message

Reply with quote

martin
Site Admin
martin avatar

Re: file list not available in .xml log file

appu wrote:

after running the script file using a .bat file .xml log is created but i am not able to find the file list in the log(by opening the .xml lof file)
Are you using the latest version of WinSCP?

Reply with quote

Advertisement

You can post new topics in this forum