Note that there are some explanatory texts on larger screens.

plurals
  1. POBatch File > Javascript > WinSCP > Check if file exists
    primarykey
    data
    text
    <p>I have a batch file that will launch a .js file which, via WinSCP, checks if a file exists and returns to the batch file if it does or not.</p> <p>The problem IS: It always returns not found, and I cannot figure out why. I am unsure how to use a wildcard in this scenario. </p> <p>The batch file looks like this:</p> <pre><code>cscript /nologo file.js if errorlevel 1 goto notfound exit :notfound (another script to copy a file over) </code></pre> <p>Only one file can exist on the server at once. So every ten min, this batch file will run, check if there is a file, if not, copy one over. </p> <p>The file.js:</p> <pre><code>// Configuration // Remote file search for var FILEPATH = "../filepath/TSS*"; // Session to connect to var SESSION = "mysession@someplace.come"; // Path to winscp.com var WINSCP = "c:\\program files (x86)\\winscp\\winscp.com"; var filesys = WScript.CreateObject("Scripting.FileSystemObject"); var shell = WScript.CreateObject("WScript.Shell"); var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml"; var p = FILEPATH.lastIndexOf('/'); var path = FILEPATH.substring(0, p); var filename = FILEPATH.substring(p + 1); var exec; // run winscp to check for file existence exec = shell.Exec("\"" + WINSCP + "\" /log=\"" + logfilepath + "\""); exec.StdIn.Write( "option batch abort\n" + "open \"" + SESSION + "\"\n" + "ls \"" + path + "\"\n" + "exit\n"); // wait until the script finishes while (exec.Status == 0) { WScript.Sleep(100); WScript.Echo(exec.StdOut.ReadAll()); } if (exec.ExitCode != 0) { WScript.Echo("Error checking for file existence"); WScript.Quit(1); } // look for log file var logfile = filesys.GetFile(logfilepath); if (logfile == null) { WScript.Echo("Cannot find log file"); WScript.Quit(1); } // parse XML log file var doc = new ActiveXObject("MSXML2.DOMDocument"); doc.async = false; doc.load(logfilepath); doc.setProperty("SelectionNamespaces", "xmlns:w='http://winscp.net/schema/session/1.0'"); var nodes = doc.selectNodes("//w:file/w:filename[@value='" + filename + "']"); if (nodes.length &gt; 0) { WScript.Echo("File found"); // signalize file existence to calling process; // you can also continue with processing (e.g. downloading the file) // directly from the script here WScript.Quit(0); } else { WScript.Echo("File not found"); WScript.Quit(1); } </code></pre> <p>On line 4 it says:</p> <pre><code>var FILEPATH = "../filepath/TSS*"; </code></pre> <p>That star is what is giving me issues, i think. I need to look for a file which STARTS WITH TSS, but will have a time stamp tacked on the end. So i need to just use a wildcard after TSS.</p> <p>So what i need help with is: Making this process return true if any file exists with TSS*</p> <p>Any help would be much appreciated. </p> <p>EDIT: </p> <pre><code>var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, 'TSS')]"); </code></pre> <p>This code seems to not work. If this code worked, it seems like it would solve all my problems.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload