Space in winscp script [duplicate] - winscp

I've written a basic .bat file script that automates downloading a group of files via WinSCP. I have used this code for performing a similar process which is working as intended, however the stumbling block appears to be the destination filepath. I have tried using
C:\Users\"John Smith"\Dropbox\joebloggs\Data\"System Data"\"Raw Feeds"\Stock\May\
and other variations with the whole filepath within inverted commas also. If anyone could advise the correct syntax for entering this filepath it would be appreciated. Code is as follows:-
#echo off
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\John Smith\Documents\WinSCP.log" /ini=nul ^
/command ^
"open sftp://joebloggs:%%40faNliGHT4#82.121.124.146/ -hostkey=""ssh-rsa 1024 01:01:dd:1c:54:72:98:c7:42:f9:15:8e:30:8c:66:3b"" -passive=0" ^
^
"get "/*.txt" "C:\Users\John Smith\Dropbox\joebloggs\Data\System Data\Raw Feeds\Stock\May\" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
Error Log is as follows:-
> 2016-05-16 13:32:43.902 Access is denied
. 2016-05-16 13:32:43.902 Asking user:
. 2016-05-16 13:32:43.902 Can't create file 'C:\Users\John.filepart'. ("System Error. Code: 5.
> 2016-05-16 13:32:43.902 Access is denied")
< 2016-05-16 13:32:43.902 Script: Can't create file 'C:\Users\John.filepart'.
< 2016-05-16 13:32:43.902 Script: System Error. Code: 5.
> 2016-05-16 13:32:43.902 Access is denied
> 2016-05-16 13:32:43.903 Type: SSH_FXP_CLOSE, Size: 23, Number: 2052
> 2016-05-16 13:32:43.903 (EScpSkipFile) Can't create file 'C:\Users\Daniel.filepart'.
> 2016-05-16 13:32:43.903 System Error. Code: 5.
> 2016-05-16 13:32:43.903 Access is denied
> 2016-05-16 13:32:43.903 Script: Failed
> 2016-05-16 13:32:43.903 Script: Exit code: 1
> 2016-05-16 13:32:43.903 Closing connection.
> 2016-05-16 13:32:43.903 Sending special code: 12
> 2016-05-16 13:32:43.903 Sent EOF message

Your have to double the double quotes. You are also missing a quote at the end of the get command (note the three consecutive quotes).
"get ""/*.txt"" ""C:\Users\John Smith\Dropbox\joebloggs\Data\System Data\...\""" ^
(path shortened so that it fits in view)
See WinSCP documentation on command-line syntax and FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
You can also have WinSCP GUI generate a SFTP batch file template for you, including correct quotes.

Related

Error appending record to a file using echo

I want to write a record using execute command stage in Datastage sequence job.
I am using below syntax.
echo "InputFileName;2021-03-25 06:54:58+01:00;AAA;Batch;FOP;FUNCTIONAL;INFO;Extra key columns sent in Key Values;201;OK;SubmitRequest;ERROR;CDIER0961E: The REST step is unable to invoke the REST service, cause=javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated;;SupplyOnhand_20210325065458;;;;0;CDIER0961E: The REST step is unable to invoke the REST service, cause=javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated;;;;12;1;2021-03-25 05:55:18+00:00;2021-03-25 05:55:33+00:00" >> Filename
Below is error I am getting.
Output from command ====>
sh: -c: line 0: syntax error near unexpected token (' sh: -c: line 0: echo Unhandled failure (-1) encountered executing command echo
I tried running this manually on linux server its working there but failing in Datastage job.
Can someone please help.
You need to escape any character that is significant to the shell, or to contain the whole string in hard (single) quotes rather than soft (double) quotes.

How WinSCP determined Errors? [duplicate]

I'm new to WinSCP. I'm trying to set an Errorlevel for the batch IM running. If the Errorlevel= 0 PRINT SUCCESS and transfers those files with Errorlevel 0 to folder call success.
if the error not equal to 0 moves the file with the error level to different folder call errors. Any suggestions.
HERE IS MY.bat
WinSCP.exe /console /script="c:\users\PDP\script.txt" /log="c:\users\PDP\lastrun.txt"
if %ERRORLEVEL% equ 0
echo Success
sendmail.exe -t < success_mail.txt
move OPTTXM* c:\users\PDP\sent ( the files in the batch start with OPTTXM*)
exit /b 0
if %ERRORLEVEL% neq 0 goto error
echo Error!
sendmail.exe -t < error_mail.txt
move ????????????????????????????????? ( how you can get inside each file and check the status
)
exit /b 1
Thanks in advance
You can make WinSCP create XML log with files that were and were not transferred. But it would be difficult (if possible at all) to parse/process those XML files in a batch file.
You better use a more powerful scripting language, like PowerShell.
See for example Move files after upload using PowerShell/WinSCP Script.
(there are many other similar questions here)
There's also WinSCP article on this topic:
Moving local files to different location after successful upload

I am skeptical about the redirection operation of below example

I was successful in redirecting the error message to a text file in the below process:
$ ls + 2>err.txt
$ cat err.txt
ls: cannot access +: No such file or directory
But when I try to attempt the same process with echo command it shows different output and unable to redirect the error message to a text file.
$ echo )hey 2>err.txt
bash: syntax error near unexpected token `)'
In your first example, it is the ls command that produces the error message that is written to err.txt.
In your second command, you are expecting the following to happen:
The line is successfully parsed
bash opens err.txt for the standard error of echo
echo tries to output )hey
echo encounters an error
The error message is written to err.txt.
However, bash never makes it past the first line, so none of 2 through 5 ever happens. Instead, the shell immediately stops processing the line and prints the error message to its own standard error.

Request user input in Linux via script and create a file according to user input

I am trying to create a script in Linux that will prompt the user to input the name of a file and according to the user's input it will create the file and put the following in the file "This is a file (filename)."
This is my attempt so far.
cat > hw5_script
echo "Enter filename:"
read filename
echo "This is a file filename"
This doesn't work for me. Just wondering what I am doing wrong? Any suggestions or input would be greatly appreciated.
You're not redirecting the output of the script to the file, and you're not using $ to expand the variable. Change the last line of the script to:
echo "This is a file $filename" > "$filename"

WinSCP won't execute the entire script specified on a command-line

My very first post but before coming here I did a research on the issues I have. Mostly on WinSCP forums but it came to no fruition.
Using winscp.com, I would like a script to use get command to download multiple files using SFTP. Here is my version:
echo ENTER WEEK NUMBER
set /p input=""
cls
Rem Create folder based on date
set year=%date:~6,4%
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_1
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_2
mkdir \\11.111.111.11\Folder1\"Folder 2"\#Folder3\%year%_WK%input%\User_3
c:
cd Program Files (x86)
cd Winscp
WinSCP.com /command ^
"#echo off" ^
"# Connect to server"^
"Open sftp://Uname:pass#999.999.999.999" ^
"# batch off mode" ^
"option batch off" ^
"lcd \\11.111.111.11\Folder1\""Folder 2""\#BFolder3\%year%_WK%input%\User_3" ^
"cd .."^
"cd RemoteFolder/USer_Folder" ^
"get %year%WK%input%.zip"^
"cd .."^
"cd User_2_Folder" ^
"get User2WK%input%.csv"^
"get SAV_LACWK%input%.csv"^
Issue is, the WinSCP executes cd .. after the first get command and it just stops. It won't go any further:
If I copy/paste at the command prompt, it works fine.
Any idea?
Thank you
Donne
All your absolute local paths use a wrong syntax C\: for a drive. The correct syntax is C:\.
mkdir C\:%year%Folder\
mkdir C\:%year%Folder\Data_User
...
WinSCP.com /command ^
....
"lcd C\:%year%Folder\Data_User" ^
You are missing a quote and an escape character (^) after
“# Change remote directory
You are using fancy quotes (“ and ”), instead of the plain double-quotes (") in:
“#Set local directory”^
....
“# Change remote directory
You have a space after the ^ in
"cd .."^
This effectively makes the ^ be ignored (it escapes the space, not the newline).
See WinSCP FAQ Why are some scripting commands specified on WinSCP command-line in a batch file not executed/failing?
If this does not help, we need to see the output of the batch file run (ideally with removed #echo off).
you're missing both the double quote and ^ at the end of the line before
change this:
“# Change remote directory
to:
"# Change remote directory" ^

Resources