Note: This tip is for use with SecureFX® for Windows and Linux.
In some file transfer scenarios, you might need to know if a particular file or subset of files exists on a remote server. The SFXCL command-line utility that is included in SecureFX for Windows and Linux can provide a complete file listing of a specified folder on a remote system. However, what if you are only interested in knowing if a specific file exists, and you don't want to manually search the results looking for a proverbial needle in a haystack of files and folders?
While SFXCL's /List functionality supports specifying a remote folder path, it doesn't currently support specifying file names, wildcards, or other patterns for filtering output. As a workaround, however, you can use the Find CMD utility to help narrow the results of SFXCL's /List output.
SFXCL's /List functionality returns a list of all files, folders and links that exist in the specified folder on a remote machine. For example:
C:/>SFXCL /List sftp://user:pass@host/target_folder
2012-02-01 09:14:30, 00001: drwxr-x--- 8192 Wed 30-Nov-2011 10:25:03 . (S)
2012-02-01 09:14:30, 00001: drwxr-xr-x 4096 Thu 04-Aug-2011 10:33:55 .. (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 540 Fri 26-Aug-2011 14:04:41 .bash_profile (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 98 Thu 30-Sep-2004 09:39:12 file.zip (S)
2011-03-01 15:51:30, 00001: -rw-r--r-- 131954 Wed 09-Feb-2005 14:56:21 rawsession.log (S)
2012-02-01 09:14:30, 00001: drwxr-xr-x 4096 Tue 08-Apr-2008 10:08:38 Scripts (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 176 Mon 07-Feb-2005 16:34:11 file3.txt (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 792 Mon 07-Feb-2005 16:34:54 file5.txt (S)
2012-02-01 09:14:30, 00001: drwxr-xr-x 4096 Tue 19-May-2009 14:25:35 vshell-installers (S)
2011-03-01 15:51:30, 00001: -rw-r--r-- 13814 Wed 07-May-2003 07:25:01 session.log (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 27 Fri 25-Feb-2011 16:07:13 myfile.sh (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 42483 Wed 03-May-2006 14:55:27 atarfile.tar (S)
The Find CMD utility can be used to exclude directories and links by filtering out all lines that don't match the pattern ": -" (the first permissions field in a long UNIX file listing displays a regular file as "-"). The following example takes the output of the SFXCL /List command and pipes the results to the Find CMD utility which filters out everything except the regular files:
C:\>SFXCL /List sftp://user@host/target_folder 2>&1 | Find ": -"
2012-02-01 09:16:38, 00001: -rw-r--r-- 540 Fri 26-Aug-2011 14:04:41 .bash_profile (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 98 Thu 30-Sep-2004 09:39:12 file.zip (S)
2011-03-01 15:51:30, 00001: -rw-r--r-- 131954 Wed 09-Feb-2005 14:56:21 rawsession.log (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 176 Mon 07-Feb-2005 16:34:11 file3.txt (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 792 Mon 07-Feb-2005 16:34:54 file5.txt (S)
2011-03-01 15:51:30, 00001: -rw-r--r-- 13814 Wed 07-May-2003 07:25:01 session.log (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 27 Fri 25-Feb-2011 16:07:13 myfile.sh (S)
2012-02-01 09:16:38, 00001: -rw-r--r-- 42483 Wed 03-May-2006 14:55:27 atarfile.tar (S)
Then you can pipe the output of the first Find command to a second instance of the Find command so as to narrow the output further. For example, to display just those files that end in ".log", you would use the following command:
SFXCL /List sftp://user@host/target_folder 2>&1 | Find ": -" | Find ".log "
The Find command returns success if the given pattern matches anything in the input (as indicated when the %ERRORLEVEL% CMD environment variable has a value of zero). For example:
C:/>SFXCL /List sftp://user@host/target_folder 2>&1 | Find ": -" | Find ".log "
2011-03-01 15:51:30, 00001: -rw-r--r-- 13814 Wed 07-May-2003 07:25:01 session.log (S)
2011-03-01 15:51:30, 00001: -rw-r--r-- 131954 Wed 09-Feb-2005 14:56:21 rawsession.log (S)
C:\>echo %ERRORLEVEL%
0
If the Find command does not match the given pattern in the input, a non-success (non-zero) value is returned. For example:
C:/>SFXCL /List sftp://user@host/target_folder 2>&1 | Find ": -" | Find ".png"
C:\>echo %ERRORLEVEL%
1
For additional information on using the SFXCL command-line utility, see the SecureFX Help files or the SFXCL Automation Guide.
VanDyke Software uses cookies to give you the best online experience. Before continuing to use this site, please confirm that you agree to our use of cookies. Please see our Cookie Usage for details.
Here you can control cookies using the checkboxes below. Some cookies are essential for the use of our website and cannot be disabled. Others provide a convenience to the user and, if disabled, may reduce the ease of use of our site. Finally, some cookies provide anonymous analytic tracking data that help us provide the user with a richer browsing experience. You can elect to disable these cookies as well.