Batch File > JavaScript > Winscp > Check If File Exists

Batch File Javascript WinSCP Check if file exists

You need to correct xpath expression in var nodes... line.
Try something like this:

doc.setProperty("SelectionLanguage", "XPath"); //added in edit
var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, '" + filename + "')]");

and delete asterisk from FILEPATH.

Note: first line is required in order to use XPath as the query language, not default (and old) XSLPattern which doesn't support methods such as starts-with or contains.

SelectionLanguage Property (MDSN).

How to check if a filename with specific pattern exists in cmd batch script?

If you only check the second character, without considering the other characters after it, you can use something like this:

for /l %%n in (1,1,9) do if exist "a%%n*" set "ANS=%%n"
if defined ANS echo yes

WinSCP time based file download

First, I assume your file does not have fixed name (contrary to your question with fixed name file.txt). If not, please explain, why do you need timestamp-based solution.


Anyway, you can use a file mask with a time constraint:

get "*.txt>2014-07-19 4:00"

To dynamically inject today's date, use the %TIMESTAMP% syntax:

get "*.txt>%TIMESTAMP#yyyy-mm-ss% 4:00"

Simply, the above means, get all files created later than 4:00 today (the %TIMESTAMP#yyyy-mm-ss% resolves to today's date in format yyyy-mm-ss, as needed for the time constraint).

When passing the get on WinSCP command-line in a batch file (using /command switch, as opposite to using /script switch to specify a separate script file), you have to double the % to avoid the batch-file trying to interpret the %TIMESTAMP%:

winscp.com /command ... "get ""*.txt>%%TIMESTAMP#yyyy-mm-ss%% 4:00"""

Another solution is a static script that rely on a relative time: E.g. you know your script runs at 6am. So you let WinSCP download all files updated/created in the last 2 hours (6am – 4am):

get *.txt>2h

See also WinSCP article on downloading the most recent file.

File says it downloaded in log, but not in local directrory

Your get command syntax says: download all files from /Inbox to local folder C:\Test\Automation and save them to file NewFolder. What effectively makes all files overwrite each other. Assuming that NewFolder folder actually does not exist. Had it existed, the download would fail with "is not file" error.

You are also for sure using some rather old version of WinSCP. As recent versions of WinSCP would warn you, that you are probably doing something wrong:

Are you sure you want to transfer multiple files to a single file 'NewFolder' in a directory 'C:\Test\Automation\'?

The files will overwrite one another.

If you actually want to transfer all files to a directory 'C:\Test\Automation\NewFolder\', keeping their name, make sure you terminate the path with a slash.

See also documentation for get command:

The last parameter specifies target local directory and optionally operation mask to store file(s) under different name. Target directory must end with backslash.


Solution:

  • Add blackslash;
  • Create NewFolder folder;
  • And you should also upgrade to the latest version of WinSCP.

Error downloading file to local subfolder using WinSCP

As @Rub mentioned, it's the spaces. You need to enclose the paths with spaces into double-quotes. Also, you need to terminate the path with backslash. Otherwise it's going to download the file to file Even Lower in local directory D:\Name\Sub Name. But I assume that you want it to download to D:\Name\Sub Name\Even Lower, keeping the name file.txt.

This is correct syntax:

get file.txt "D:\Name\Sub Name\Even Lower\"

Some references:

https://winscp.net/eng/docs/scripting#syntax

https://winscp.net/eng/docs/scriptcommand_get

The error message you are getting does not make much sense. When trying the same, I'm getting:

Can't get attributes of file 'D:\Name\Sub'

No such file or directory.

That makes sense as your command means: Download three remote files file.txt, D:\Name\Sub and Name\Even to local file Lower in your local current working directory (overwritting one another). So it fails finding remote file D:\Name\Sub.



Related Topics



Leave a reply



Submit