Topic "Create basic filelist using XML-logging and vbs script"

Author Message
ciove
[View user's profile]

Joined: 2011-06-22
Posts: 9

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:

Quote:

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:

Quote:

winscp.com /log=transfer_log.xml /script=myscript.txt


The myscript.txt looks like this:

Quote:

open sftp://myaccount:MyPa55word@myserver.example.com
ls
exit



The VBS script

Code:

' 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
Quote:

cscript //nologo list_files_in_winscp_xml-log.vbs transfer_log.xml > filelist.txt


The filelist.txt now contains something like this:
Quote:

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:
Quote:

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:
Code:

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
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Thanks for sharing this!
_________________
Martin Prikryl
DuckAlumni

Guest


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:
Quote:

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:
Quote:

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.
appu

Guest


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
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
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?
_________________
Martin Prikryl
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