Search code examples
scriptingwinscp

WinSCP ls command with file mask is logging all files


I am trying to do a simple ls via WinSCP script. There are many files on the FTP (in the format TOE#####.###, where are # are numbers) and I only want the ones that begin with TOE54281 in the folder TOEFL.

I've tried ls TOEFL/TOE54281*, ls TOEFL/TOE54281*.???, ls TOEFL/*54281* (and other alternatives), but all give me all the file contents, not just the ones beginning with what I need.

Any suggestions?

2015-01-23 13:23:43.878 Script: ls TOEFL/TOE52481*
. 2015-01-23 13:23:43.878 Listing directory "/TOEFL".
2015-01-23 13:23:43.878 Type: SSH_FXP_OPENDIR, Size: 15, Number: 267
< 2015-01-23 13:23:43.910 Type: SSH_FXP_HANDLE, Size: 13, Number: 267
2015-01-23 13:23:43.910 Type: SSH_FXP_READDIR, Size: 13, Number: 524
...
< 2015-01-23 13:23:50.618 Type: SSH_FXP_STATUS, Size: 28, Number: 58892
< 2015-01-23 13:23:50.618 Status code: 1
2015-01-23 13:23:50.618 Type: SSH_FXP_CLOSE, Size: 13, Number: 59140
. 2015-01-23 13:23:50.634 .;d;603136;2015-01-23T16:41:21.000Z;"4433" [4433];"15002" [15002];rwxr-xr-x;2
. 2015-01-23 13:23:50.634 ..;d;1024;2014-12-06T13:26:58.000Z;"0" [0];"0" [0];rwxr-xr-x;0
. 2015-01-23 13:23:50.634 TOE58183.TST;-;1493;2014-11-18T16:20:02.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:23:50.634 TOR84390.099;-;5409;2014-10-09T14:12:15.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:23:50.634 TOE84390.TST;-;3792;2014-10-06T20:31:30.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:23:50.634 TOE50072.TST;-;3792;2014-10-06T20:37:39.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
...
. 2015-01-23 13:24:07.389 TOE98410.023;-;819;2014-12-05T16:16:27.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:24:07.389 TOE98600.023;-;1769;2014-12-05T16:16:28.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:24:07.389 TOE99430.023;-;998244;2014-12-05T16:16:28.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:24:07.389 TOE99640.023;-;3756;2014-12-05T16:16:29.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
. 2015-01-23 13:24:07.389 TOE99881.023;-;3890;2014-12-05T16:16:29.000Z;"4433" [4433];"4433" [4433];rw-rw-r--;0
< 2015-01-23 13:24:07.545 Script: -rw-rw-r-- 1 4433 4433 415685 Oct 10 11:55:26 2014 TOE52481.015
< 2015-01-23 13:24:07.545 Script: -rw-rw-r-- 1 4433 4433 393699 Oct 17 9:39:41 2014 TOE52481.016
< 2015-01-23 13:24:07.545 Script: -rw-rw-r-- 1 4433 4433 402250 Sep 26 10:13:23 2014 TOE52481.013
< 2015-01-23 13:24:07.545 Script: -rw-rw-r-- 1 4433 4433 559835 Oct 24 10:28:07 2014 TOE52481.017
< 2015-01-23 13:24:07.545 Script: -rw-rw-r-- 1 4433 4433 605034 Oct 31 11:24:01 2014 TOE52481.018


Solution

  • Your first two masks are correct:

    winscp> ls TOEFL
    D---------   0                           0              ..
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE99999.222
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.111
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.222
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE99999.111
    winscp> ls TOEFL/TOE54281*
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.111
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.222
    winscp> ls TOEFL/TOE54281*.???
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.111
    -rw-r--r--   0 user1    users           10 Jan 23 17:58:07 2015 TOE54281.222
    

    References:


    But it seems that you actually refer to listing in a WinSCP session log, rather than a listing on script console (= result of the ls command).

    The SFTP or FTP protocols do not provide any mechanism for a client to request listing of files matching certain criteria only (let alone matching WinSCP-specific file mask).

    (Note that you are using the SFTP protocol, contrary to your post. But for the FTP protocol, all this is equally true)

    So WinSCP (or any other SFTP or FTP client) has to request the server to provide a complete listing of a directory, and then locally filter that list for files matching the criteria (the mask).

    What you see in the session log, is whole process that leads to final results, which is listing on the script console. That listing is marked in the log by "Script:" prefix.

    Please note that the session log is not intended for any kind of automatic processing/parsing, if that was what you wanted to do. If you need to parse the results, use XML log, particularly its ls tag. It includes only matching files and is intended for automatic processing/parsing:

    <?xml version="1.0" encoding="UTF-8"?>
    <session xmlns="http://winscp.net/schema/session/1.0" name="..." start="2015-01-23T20:48:29.696Z">
      ...
      <ls>
        <destination value="/home/user1/TOEFL" />
        <files>
          <file>
            <filename value="TOE54281.111" />
            <type value="-" />
            <size value="10" />
            <modification value="2015-01-23T16:58:07.000Z" />
            <permissions value="rw-r--r--" />
            <owner value="user1" />
            <group value="users" />
          </file>
          <file>
            <filename value="TOE54281.222" />
            <type value="-" />
            <size value="10" />
            <modification value="2015-01-23T16:58:07.000Z" />
            <permissions value="rw-r--r--" />
            <owner value="user1" />
            <group value="users" />
          </file>
        </files>
        <result success="true" />
      </ls>
    </session>