Topic "set remote directory when openning session"

Author Message
Williet
[View user's profile]

Joined: 2015-02-12
Posts: 2
Location: Scotland
I created a script to download the latest file from a remote directory, which works fine when I used the saved session in WinSCP. However when I tried without using the session it fails as the initial remote directory needs set. How can I set the initial remote directory in (or before) the open session.

The open session is achieved by:
// run winscp to get list of file in the remote directory into XML log
exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\"");
exec.StdIn.Write(
"option batch abort\n" +
"option confirm off\n" +
"open \"" + SESSION + "\"\n" +
"exit\n");

I have removed the line "ls \"" + REMOTEPATH + FILEMASK + "\"\n" +
which gets the directory listing to demonstrate the error just comes from the open command.
The ls command does look for an error in case there are no files, but as the open gives the same error the code stops.
Alternatively if I can't set the directory how can I differentiate between the error 2 from the open, and an error 2 if the ls finds no files?

The following is from the .log file. It shows the successful connection, but then gives an error as the current 'root' directory is not one we are authorised to see.
In the interactive GUI I can set the Remote Directory to use when it starts, how can I achieve the same with a script and no existing session?

*** log ***
2015-02-12 10:55:30.381 Access granted
. 2015-02-12 10:55:30.396 Opening session as main channel
. 2015-02-12 10:55:30.443 Opened main channel
. 2015-02-12 10:55:30.708 Started a shell/command
. 2015-02-12 10:55:30.708 --------------------------------------------------------------------------
. 2015-02-12 10:55:30.708 Using SFTP protocol.
. 2015-02-12 10:55:30.708 Doing startup conversation with host.
> 2015-02-12 10:55:30.708 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2015-02-12 10:55:30.755 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2015-02-12 10:55:30.755 SFTP version 3 negotiated.
. 2015-02-12 10:55:30.755 We believe the server has signed timestamps bug
. 2015-02-12 10:55:30.755 We will use UTF-8 strings when appropriate
. 2015-02-12 10:55:30.755 Getting current directory name.
. 2015-02-12 10:55:30.755 Getting real path for '.'
> 2015-02-12 10:55:30.755 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2015-02-12 10:55:30.802 Type: SSH_FXP_STATUS, Size: 33, Number: 16
< 2015-02-12 10:55:30.802 Status code: 2, Message: 16, Server: File not found, Language: en
* 2015-02-12 10:55:30.802 (ECommand) Error getting name of current remote directory.
* 2015-02-12 10:55:30.802 Cannot get real path for '.'.
* 2015-02-12 10:55:30.802 No such file or directory.
* 2015-02-12 10:55:30.802 Error code: 2
* 2015-02-12 10:55:30.802 Error message from server (en): File not found
. 2015-02-12 10:55:30.802 Startup conversation with host finished.
*** log ends ***
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24552
Location: Prague, Czechia
In scripting you use cd and lcd commands to change current remote and local working directories.
http://winscp.net/eng/docs/scriptcommand_cd
http://winscp.net/eng/docs/scriptcommand_lcd
If you really need to start in a specific remote directory straight away (not needed generally), you can specify that in session URL with open command like open sftp://user@host/initial/path/to/start/in
_________________
Martin Prikryl
Williet
[View user's profile]

Joined: 2015-02-12
Posts: 2
Location: Scotland
prikryl wrote:
In scripting you use cd and lcd commands to change current remote and local working directories.
http://winscp.net/eng/docs/scriptcommand_cd
http://winscp.net/eng/docs/scriptcommand_lcd
If you really need to start in a specific remote directory straight away (not needed generally), you can specify that in session URL with open command like open sftp://user@host/initial/path/to/start/in


Thanks for your assistance.
The change directory works but as there has already been an error in the script the script fails at the first error check.

I used the saved session mechanism as that can connect to a predefined directory and works fine from a script / scheduled job too.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24552
Location: Prague, Czechia
Williet wrote:
The change directory works but as there has already been an error in the script the script fails at the first error check.

The URL solution works for this.
_________________
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