Check if File Exists - Wildcard Help

Advertisement

CBW
Guest

Check if File Exists - Wildcard Help

I'm using the code provided here: https://winscp.net/eng/docs/script_checking_file_existence

I have it working fine, the problem is: I have a server that spits out a file that starts with TSS and then tacks on a time stamp. No one file is the same name. So what i don't understand is how to find ANY FILE that starts with TSS and return found if it exists. A typical file name would be TSS_050213_121301.

// 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 > 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);
}

On line 4 i have
var FILEPATH = "../filepath/TSS*";
and im pretty dang sure thats not the way to do a wildcard in this situation.

Any help would be much appreciated.

Reply with quote

Advertisement

CBW
Guest

I have also tried changing out the code on line 69:

var nodes = doc.selectNodes("//w:file/w:filename[starts-with[@value='" + filename + "']]");

Still, no luck. Is there a way to search for just TSS* in the xml?

Reply with quote

CBW
Guest

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

That'll fix the wildcard problem. In case anyone ever cares...

Reply with quote

deester
Guest

session.FileExists

In my SSIS script task, I am trying to see if I have any files on my SFTP site prior to running any additional processes, The file I want to transfer will have a datetime stamp appended to the end of the file name (so I will never know what the whole file name will be).
It appears that the .FileExists method does not work if you have a wild card in your remote path file name, i.e. //path/Oshi_rs*

if (session.FileExists(remotePath))
{
(bool)Dts.Variables["User::fileExists"].Value = true;
}

And using session.ListDirectory(remoteDir) returns more than a count of one when I know that there is only one file there:

RemoteDirectoryInfo directory = session.ListDirectory(remoteDir);
foreach (RemoteFileInfo fileInfo in directory.Files)
{
recordCount += 1;
}

The .ListDirectory is listing ., .. root paths as files.

Is there any way to use wildcards with .FileExists or to get only 'files' and not directories with .ListDirectory (or some other more applicable method)?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: session.FileExists

deester wrote:

...to get only 'files' and not directories with .ListDirectory (or some other more applicable method)?
What about something as simple as:

using System.Linq;
...

foreach (RemoteFileInfo fileInfo in directory.Files.Where(file => !file.IsDirectory))

Reply with quote

Guest

FileExits in Commandline mode

Hi,

NewBee Winscp here with actually a couple of questions :

1. Is there a solution to do a fileexits check but then on de "command line" version?
2. I see that it's possible in vb.net or java.... but how about vba?
3. in C# and vb.net I see it's only possible to directly put accountname and password in the options. But I want to use the already registered session/details in WinSCP instead of putting this information in the actual script.

Thank you in advance ...... please bare with me as these are newbee questions.

Regards,
Sudhier

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: FileExits in Commandline mode

1. Is there a solution to do a fileexits check but then on de "command line" version?
2. I see that it's possible in vb.net or java.... but how about vba?
The Session.FileExists method internally uses scripting command stat:
https://winscp.net/eng/docs/scriptcommand_stat
If that succeeds, file exists.

Though in VBA, you can still use WinSCP .NET assembly via COM interface:
https://winscp.net/eng/docs/library_vb

3. in C# and vb.net I see it's only possible to directly put accountname and password in the options. But I want to use the already registered session/details in WinSCP instead of putting this information in the actual script.
That's not a good practice and your should avoid it.
See https://winscp.net/eng/docs/scripting#configuration

Reply with quote

Advertisement

You can post new topics in this forum