Batch File > Javascript > WinSCP > Check if file exists

醉酒当歌 提交于 2019-12-01 13:41:38

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

You can use the stat command. You can even inline the WinSCP script into the batch file:

@echo off

set REMOTE_PATH=/home/user/test.txt
winscp.com /command ^
    "option batch abort" ^
    "open mysession" ^ 
    "stat %REMOTE_PATH%" ^ 
    "exit"

if errorlevel 1 goto error

echo File %REMOTE_PATH% exists
rem Do something
exit 0

:error
echo Error or file %REMOTE_PATH% not exists
exit 1

An alternative is using the Session.FileExists from WinSCP .NET assembly.


For further details, see the WinSCP article Checking file existence.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!