Uploading a file or handover a batch variable to script

Advertisement

Guest

Uploading a file or handover a batch variable to script

Hey guys,

since a few days im trying to create a batch script which does the following:

- Take alle csv files within a folder

- Zip the files with 7z and create a archive with a timestamp

- Upload the zip file over sftp to a server

The first and secound part is done. Now ive the problem that my statement for the file upload doesent work.
Ive done the upload within a script file but there are my problem that i coudnt find anything how to handover the variable with the filename of the zip file.

Heres the code:

@echo off

rem create timestamp
FOR /F "tokens=1,2,3 delims=/. " %%a in ('echo %date%') do set FDATE=%%c%%b%%a
FOR /F "tokens=1,2,3 delims=/: " %%a in ('echo %time%') do set FTIME=%%a%%b%%c

set archive=Archive_%FDATE%_%FTIME%.zip

rem loop over all files
for /r "C:\Temp\" %%a in (*.csv) do (  

   echo %%a

   rem add file to the archive
   "C:\Temp\7-Zip64\7z.exe" a -tzip %archive% %%a
   
   rem deleate file
   rem del %%a
)


"C:\Temp\WinSCP\winscp.exe" /command "option confirm off" "open sftp://TEMPUSER@xx.xx.xx.xx:xxxx -privatekey="C:\Temp\TEMPKEY.ppk"" "put %archive% \HOME\TEMP\"

echo Upload beendet

pause

There is no error message. In case im loading up all the *.zip files with the following script everything works fine. But i just want to upload the single file.

Thats the "script" part i cannot expand to use the variable %archive%:

# Disable overwrite confirmations that conflict with the previous
option confirm off

# Connect using a password
open sftp://TEMP@xx.xx.xx.xx:xxxx-privatekey="C:\Temp\TEMPKEY.ppk"

# Change remote directory
cd \HOME\TEMP\

# Change the Local Directory
lcd "C:\Temp\"

# Force binary mode transfer
option transfer binary

# Upload the files to current working directory
# Nice would be "put %archive%
put "*.zip"

# Disconnect
close

# Exit WinSCP
exit

I hope you can help me.

Kings regards

Marc

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: Uploading a file or handover a batch variable to script

You can use environment variables in the script.

Anyway, please attach a full log file showing the problem (using the latest version of WinSCP). Either for the command-line approach or the script one or both.

To generate log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you may email it to me. You will find my address (if you log in) in my forum profile. Please include link back to this topic in your email. Also note in this topic that you have emailed the log.

Reply with quote

Guest

My problem is that i either has to know why this line doesent work:

"C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\winscp.exe" /command "option confirm off" "open sftp://MEDICONKUNDEN@84.16.232.20:1970" "-privatekey=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\MEDICONKUNDEN.ppk" "put C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\Archive_20131009_151707.zip \HOME\MEDICONKUNDEN\" "/log=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\log_justupload.txt"

I dont know how it will help you but heres the log:
. 2013-10-10 14:03:40.583 --------------------------------------------------------------------------
. 2013-10-10 14:03:40.583 WinSCP Version 5.1.7 (Build 3446) (OS 5.2.3790 Service Pack 2)
. 2013-10-10 14:03:40.583 Configuration: C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\WinSCP.ini
. 2013-10-10 14:03:40.583 Local account: CENSORED
. 2013-10-10 14:03:40.583 Working directory: C:\Dokumente und Einstellungen\Administrator\Desktop\Temp
. 2013-10-10 14:03:40.583 Process ID: 3744
. 2013-10-10 14:03:40.583 Command-line: "C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\winscp.exe" /command "option confirm off" "open sftp://CENSORED@CENSORED:CENSORED" "-privatekey=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\CENSORED.ppk" "put C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\Archive_20131009_151707.zip \HOME\CENSORED\" "/log=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\log_justupload.txt"
. 2013-10-10 14:03:40.583 Time zone: Current: GMT+2, Standard: GMT+1, DST: GMT+2, DST Start: 31.03.2013, DST End: 27.10.2013
. 2013-10-10 14:03:40.583 Login time: Donnerstag, 10. Oktober 2013 14:03:40
. 2013-10-10 14:03:40.583 --------------------------------------------------------------------------
. 2013-10-10 14:03:40.583 Session name: put C (Ad-Hoc session)
. 2013-10-10 14:03:40.583 Host name: put C (Port: -1)
. 2013-10-10 14:03:40.583 User name:  (Password: No, Key file: Yes)
. 2013-10-10 14:03:40.583 Tunnel: No
. 2013-10-10 14:03:40.583 Transfer Protocol: SFTP (SCP)
. 2013-10-10 14:03:40.583 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2013-10-10 14:03:40.583 Proxy: none
. 2013-10-10 14:03:40.583 SSH protocol version: 2; Compression: No
. 2013-10-10 14:03:40.583 Bypass authentication: No
. 2013-10-10 14:03:40.583 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2013-10-10 14:03:40.583 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2013-10-10 14:03:40.583 SSH Bugs: A,A,A,A,A,A,A,A,A,A
. 2013-10-10 14:03:40.583 Return code variable: Autodetect; Lookup user groups: A
. 2013-10-10 14:03:40.583 Shell: default
. 2013-10-10 14:03:40.583 EOL: 0, UTF: 2
. 2013-10-10 14:03:40.583 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2013-10-10 14:03:40.583 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2013-10-10 14:03:40.583 SFTP Bugs: A,A
. 2013-10-10 14:03:40.583 SFTP Server: default
. 2013-10-10 14:03:40.583 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2013-10-10 14:03:40.583 Cache directory changes: Yes, Permanent: Yes
. 2013-10-10 14:03:40.583 DST mode: 1; Timezone offset: 0h 0m
. 2013-10-10 14:03:40.583 --------------------------------------------------------------------------
. 2013-10-10 14:03:40.583 Looking up host "put C"
. 2013-10-10 14:03:53.833 --------------------------------------------------------------------------
. 2013-10-10 14:03:53.833 WinSCP Version 5.1.7 (Build 3446) (OS 5.2.3790 Service Pack 2)
. 2013-10-10 14:03:53.833 Configuration: C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\WinSCP.ini
. 2013-10-10 14:03:53.833 Local account: CENSORED
. 2013-10-10 14:03:53.833 Working directory: C:\Dokumente und Einstellungen\Administrator\Desktop\Temp
. 2013-10-10 14:03:53.833 Process ID: 3744
. 2013-10-10 14:03:53.833 Command-line: "C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\winscp.exe" /command "option confirm off" "open sftp://CENSORED@CENSORED:CENSORED" "-privatekey=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\CENSORED.ppk" "put C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\Archive_20131009_151707.zip \HOME\FOLDER\" "/log=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\log_justupload.txt"
. 2013-10-10 14:03:53.833 Time zone: Current: GMT+2, Standard: GMT+1, DST: GMT+2, DST Start: 31.03.2013, DST End: 27.10.2013
. 2013-10-10 14:03:53.833 Login time: Donnerstag, 10. Oktober 2013 14:03:53
. 2013-10-10 14:03:53.833 --------------------------------------------------------------------------
. 2013-10-10 14:03:53.833 Session name: CENSORED@CENSORED (Ad-Hoc session)
. 2013-10-10 14:03:53.833 Host name: CENSORED (Port: CENSORED)
. 2013-10-10 14:03:53.833 User name: CENSORED (Password: No, Key file: No)
. 2013-10-10 14:03:53.833 Tunnel: No
. 2013-10-10 14:03:53.833 Transfer Protocol: SFTP
. 2013-10-10 14:03:53.833 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2013-10-10 14:03:53.833 Proxy: none
. 2013-10-10 14:03:53.833 SSH protocol version: 2; Compression: No
. 2013-10-10 14:03:53.833 Bypass authentication: No
. 2013-10-10 14:03:53.833 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2013-10-10 14:03:53.833 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2013-10-10 14:03:53.833 SSH Bugs: A,A,A,A,A,A,A,A,A,A
. 2013-10-10 14:03:53.833 Return code variable: Autodetect; Lookup user groups: A
. 2013-10-10 14:03:53.833 Shell: default
. 2013-10-10 14:03:53.833 EOL: 0, UTF: 2
. 2013-10-10 14:03:53.833 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2013-10-10 14:03:53.833 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2013-10-10 14:03:53.833 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2013-10-10 14:03:53.833 Cache directory changes: Yes, Permanent: Yes
. 2013-10-10 14:03:53.833 DST mode: 1; Timezone offset: 0h 0m
. 2013-10-10 14:03:53.833 --------------------------------------------------------------------------
. 2013-10-10 14:03:53.833 Looking up host "CENSORED"
. 2013-10-10 14:03:53.833 Connecting to CENSORED port CENSORED
. 2013-10-10 14:03:54.052 Server version: SSH-2.0-OpenSSH_4.3
. 2013-10-10 14:03:54.052 Using SSH protocol version 2
. 2013-10-10 14:03:54.052 We claim version: SSH-2.0-WinSCP_release_5.1.7
. 2013-10-10 14:03:54.145 Doing Diffie-Hellman group exchange
. 2013-10-10 14:03:54.411 Doing Diffie-Hellman key exchange with hash SHA-1
. 2013-10-10 14:03:54.958 Verifying host key rsa2 CENSORED with fingerprint ssh-rsa 2048 CENSORED
. 2013-10-10 14:03:54.958 Host key matches cached key
. 2013-10-10 14:03:54.958 Host key fingerprint is:
. 2013-10-10 14:03:54.958 ssh-rsa 2048 CENSORED
. 2013-10-10 14:03:54.958 Initialised AES-256 SDCTR client->server encryption
. 2013-10-10 14:03:54.958 Initialised HMAC-SHA1 client->server MAC algorithm
. 2013-10-10 14:03:54.958 Initialised AES-256 SDCTR server->client encryption
. 2013-10-10 14:03:54.958 Initialised HMAC-SHA1 server->client MAC algorithm
. 2013-10-10 14:03:55.270 Reading private key file "C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\CENSORED.ppk"
! 2013-10-10 14:03:55.270 Using username "CENSORED".
. 2013-10-10 14:03:55.333 Offered public key
. 2013-10-10 14:03:55.708 Offer of public key accepted
! 2013-10-10 14:03:55.708 Authenticating with public key "imported-openssh-key"
. 2013-10-10 14:03:56.052 Sent public key signature
. 2013-10-10 14:03:56.364 Access granted
. 2013-10-10 14:03:56.520 Opened channel for session
. 2013-10-10 14:03:56.848 Started a shell/command
. 2013-10-10 14:03:56.848 --------------------------------------------------------------------------
. 2013-10-10 14:03:56.848 Using SFTP protocol.
. 2013-10-10 14:03:56.848 Doing startup conversation with host.
> 2013-10-10 14:03:56.848 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2013-10-10 14:03:57.286 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2013-10-10 14:03:57.286 SFTP version 3 negotiated.
. 2013-10-10 14:03:57.286 We believe the server has signed timestamps bug
. 2013-10-10 14:03:57.286 We will use UTF-8 strings for status messages only
. 2013-10-10 14:03:57.286 Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
. 2013-10-10 14:03:57.286 Getting current directory name.
. 2013-10-10 14:03:57.286 Getting real path for '.'
> 2013-10-10 14:03:57.286 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2013-10-10 14:03:57.380 Type: SSH_FXP_NAME, Size: 59, Number: 16
. 2013-10-10 14:03:57.380 Real path is '/home/CENSORED'
. 2013-10-10 14:03:57.380 Startup conversation with host finished.
< 2013-10-10 14:03:57.380 Script: Active session: [1] CENSORED@CENSORED
. 2013-10-10 14:03:57.380 Script: Exit code: 1
. 2013-10-10 14:03:57.380 Closing connection.
. 2013-10-10 14:03:57.380 Sending special code: 12
. 2013-10-10 14:03:57.380 Sent EOF message

or that i need an example how i can get a variable in the script. Something like that:
(I dont think that this will work but how can i get the %archive% variable from the batch script in the winscp script file?)

Batch file:
set archive=NameOfData.zip

start "C:\Temp\" winscp.exe /console /script=Script.txt /%archive%"

Script file:
# Disable overwrite confirmations that conflict with the previous
option confirm off

# Connect using a password
open sftp://CENSORED@xx.xx.xx.xx:xxxx -privatekey="C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\PRIVATEKEYFILE.ppk"

# Change remote directory
cd \HOME\FOLDER\

# Change the Local Directory
lcd "C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\"

# Force binary mode transfer
option transfer binary

# Upload the files to current working directory
put "%archive%"

# Disconnect
close

# Exit WinSCP
exit

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Anonymous wrote:

My problem is that i either has to know why this line doesent work:

"C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\WinSCP\winscp.exe" /command "option confirm off" "open sftp://MEDICONKUNDEN@84.16.232.20:1970" "-privatekey=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\MEDICONKUNDEN.ppk" "put C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\Archive_20131009_151707.zip \HOME\MEDICONKUNDEN\" "/log=C:\Dokumente und Einstellungen\Administrator\Desktop\Temp\log_justupload.txt"
You have quotes wrong for the -privatekey, what completely confuses a command-line parser.
See https://winscp.net/eng/docs/commandline#syntax

or that i need an example how i can get a variable in the script. Something like that:
(I dont think that this will work but how can i get the %archive% variable from the batch script in the winscp script file?)

Batch file:
set archive=NameOfData.zip

start "C:\Temp\" winscp.exe /console /script=Script.txt /%archive%"

You do not need to do anything to pass an environment variable into script. It should just work. Just remove the /%archive% part (which breaks it).
https://winscp.net/eng/docs/scripting#syntax

Reply with quote

Advertisement

You can post new topics in this forum