Topic "Check if File Exists - Wildcard Help"

Author Message
CBW

Guest


I'm using the code provided here: http://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.

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 > 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
Code:
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.
Advertisements
CBW

Guest


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

Code:
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?
CBW

Guest


Place
Code:
doc.setProperty("SelectionLanguage", "XPath");

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


That'll fix the wildcard problem. In case anyone ever cares...
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
You should better use WinSCP .NET assembly and its Session.FileExists method:
http://winscp.net/eng/docs/library
http://winscp.net/eng/docs/library_session_fileexists
_________________
Martin Prikryl
deester

Guest


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)?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
deester wrote:
...to get only 'files' and not directories with .ListDirectory (or some other more applicable method)?

What about something as simple as:

Code:
using System.Linq;
...

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




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
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Quote:
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:
http://winscp.net/eng/docs/scriptcommand_stat
If that succeeds, file exists.

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

Quote:
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 http://winscp.net/eng/docs/scripting#configuration
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License