Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: How to check file exists or not

vanasdj wrote:

stat does not support file masks.

Why do you even need to check if the files exist?
Just use the get .../*.xml straight away.

Sorry, I have overwritten your post, instead of responding.
vanasdj

Re: How to check file exists or not

stat does not support file masks.

Why do you even need to check if the files exist?
Just use the get .../*.xml straight away.
Ahmed HADDAD

May be this helps

Even if it is an old post, I think it is never toooo late!
I tried the following inside the script file (after connexion):
call echo -d /directory_path/ && mkdir /directory_path/

You will have error messages in the case the /directory_path/ already exists. But the scripts continue running.

Wish it will help.
Guest

Re: File ready exisit check

I have been trying to use this script to check for the presence of a file in the remote directory, but the "ls" command is unable to execute successfully.

The remote file does exist at the root, so I'm wondering if there is an issue in defining the target remote directory as "/". Perhaps there is a syntax handling problem?

Can anyone suggest a possible workaround??

Many thanks!
martin

Re: File ready exisit check

Sowmya wrote:

So i am having
WinSCP.StandardInput.WriteLine("mkdir " & strTargetFolder)
i have error condition and also i have "option batch continue" at batch level.
But the code just hangs in the
output = WinSCP.StandardOutput.ReadToEnd()
' Wait until WinSCP finishes
WinSCP.WaitForExit()
can any one of you suggest how to check the existense of the folder,before creating it on remote site?

Please post your full script.
Sowmya

File ready exisit check

Hi,

I have similar requirements,where in i need to have a condition if folder "XX" already exisit in remote site then dont create new folder else create "XX".

So i am having
WinSCP.StandardInput.WriteLine("mkdir " & strTargetFolder)
i have error condition and also i have "option batch continue" at batch level.
But the code just hangs in the
output = WinSCP.StandardOutput.ReadToEnd()
' Wait until WinSCP finishes
WinSCP.WaitForExit()
can any one of you suggest how to check the existense of the folder,before creating it on remote site?

the help file provided in this site does not help.

Thanks
Sowmya
M.R.

incorrect script

I tested this approach in VB.Net but it does not work. LS command does not seem to be able to check if a file exists.

I am getting too a 'file not found' error regardless file exists or not...

M.R.
martin

Re: re: script issue

What does the log file look like?
webmd_tech

re: script issue

I have found a workaround...

//https://winscp.net/forum/viewtopic.php?p=28228#28228
// Configuration
// Remote file search for
var FILEPATH = "TestFile_DI2010A.txt";
// Session to connect to
var SESSION = "FTP01T";
// Path to winscp.com
var WINSCP = "c:\\program files\\winscp\\winscp.com";

var filesys = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

//var logfilepath = "D:\\autodialer_get" + "\\" + filesys.GetTempName() + ".xml";
var logfilepath = "D:\\autodialer_get\\autodialer_check.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\n" +
"exit\n");

exec.StdIn.Close();

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

var forReading = 1, forWriting = 2, forAppending = 8;
// define array to store lines.
rline = new Array();

// Create the object
fs = new ActiveXObject("Scripting.FileSystemObject");
f = fs.GetFile(logfilepath);

// Open the file for input.
is = f.OpenAsTextStream(forReading, 0);

// start and continue to read until we hit
// the end of the file.
var count = 0;
while( !is.AtEndOfStream ){
rline[count] = is.ReadLine();
count++;
}

// Close the stream
is.Close();

// Place the contents of the array into
// a variable.
var msg = "";
for(i = 0; i < rline.length; i++){
msg += rline[i] + "\n";
}

var matchPos1 = msg.search(FILEPATH);

if(matchPos1 != -1)
{
//File Exists
var wsh = new ActiveXObject('WScript.Shell');
wsh.Run("another_script");}
else
{
//File Does Not Exist
WScript.Quit(1);}
webmd_tech

re: script issue

Here is the script I am using:
*********************************************
// Configuration

// Remote file search for
var FILEPATH = "TestFile_DI2010A.txt";
// Session to connect to
var SESSION = "[remoteserver]";
// Path to winscp.com
var WINSCP = "c:\\program files\\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\n" +
"exit\n");

exec.StdIn.Close();

// 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='https://winscp.net/schema/session/1.0'");

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

WScript.Echo(nodes.path);

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 fron the script here
WScript.Quit(0);
}
else
{
WScript.Echo("File not found");
WScript.Quit(1);
}
*********************************************

Here is the runtime:
*********************************************
D:\autodialer_get>cscript autodialer_get.js
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

winscp> option batch abort
batch abort
winscp> open "[remoteserver]"
Searching for host...
Connecting to host...
Authenticating...
Using username "[username]".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] FTP01T
winscp> ls
Drwxr-x--- 0 0 Jan 18 2:54:59 2010 .
Drwxr-x--- 0 0 Jan 15 5:38:37 2010 ..
-rwxr-x--- 0 26 Jan 18 1:53:15 2010 TestFile_DI2010A.txt
winscp> exit

File not found
*********************************************

When I open WinSCP and connect I am automatically dropped into the root directory where TestFile_DI2010A.txt is but the script states that it can't find it.
martin

Re: How to check file exists or not

webmd_tech wrote:

I've been testing this script and it doesn't work. I gather that this is suppose to write the results of the "ls" command to the log file but that is not happening. Every time I run this I get the File not found error.

Can you give us more details? What do you run? Can you post a screenshot?
webmd_tech

Re: How to check file exists or not

martin wrote:

I have added a guide for checking for file existence.


I've been testing this script and it doesn't work. I gather that this is suppose to write the results of the "ls" command to the log file but that is not happening. Every time I run this I get the File not found error.
martin

Re: How to check file exists or not

This guide contains example for similar problem. I hope it helps you.
Saity

Re: How to check file exists or not

Thanks for the reply martin, but my requirement is a bit different, let me explain you that, suppose I need to download multiple files, like ABC.txt, bdc.txt and efg.xls by using the same script, so here I have to check if that particular file exists then only download otherwise log the error that file dose not exists, so for that I need to check the existence of the files, please help me out how can I achieve that with the same script, I really appreciate your help. Thanks a ton..
martin

Re: How to check file exists or not

If the file does not exist, the script fails, so you can catch that already.
Saity

How to check file exists or not

Hi,
I am using below script to get the file from the ftp location, I am using winscp in my execute process task of SSIS.

option batch on
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect using a password
# open user:password@example.com
# Connect
open user@example.com
# Change remote directory
cd /home/user
# Force binary mode transfer
option transfer binary
# Download file to the local directory d:\
get examplefile.txt d:\
# Disconnect
close
exit

Now befor I start to get the file in my local system I want to check that specific file or file extension actually exists in ftp, if not then want to catch that error.
So for this what are the changes I have to do in my current script. Please let me know, I really appriciate the help for the same.
martin

Re: Check if file EXISTS before Downloading

Looks like you are curing consequence instead of cause.

So, why does not your script hang? Haven't you omitted the "option batch on" command?

Please read FAQ.
dfalconbury

Check if file EXISTS before Downloading

I am new to WINSCP and therefore still learning the scripting commands. Is there a way to check if a file exists on a remote machine before downloading it using winscp.com with /command? I have automated my downloads and if the file does not exist, my process "hangs." I would like to check if the file exists before attempting to download. If the file exists, the script would continue, otherwise it would exit.

Any help you can provide would be greatly appreciated.