Batch file when using WinSCP and command prompt - batch-file

I am currently writing a script that calls WinSCP, connects an SFTP session, transfers a group of files from a local server to a remote server, closes the connection, then moves the local file to an archive. An alternate acceptable solution would be to copy the file to archive and then delete it from the source directory.
The problem I am having is that I can get WinSCP to open, connect and transfer the files, but after that the commands are ignored and I am unable to automate the connection close, file move or copy and the deletion and closing of command prompt. What is the best way to do this?
My FTP.bat file that connects the session and calls the script -
"C:\Program Files\WinSCP\WinSCP.exe" /console /command "lcd d:\bofa_ftp\out" /script=script.txt savedsession#winscpname.com
My script file that puts the file and closes the connection -
option batch abort
option confirm off
option exclude script.txt
put *.txt
close
When I add any commands to either the bat or the script they are ignored.

Commands after close are definitely not ignored. Your problem might be that you are trying to put Windows commands to WinSCP script. Note that there are no commands in WinSCP to move local files.

Instead of trying using Winscp3, try with plink.exe.
Plink is the best option to transfer files from winscp3 to your local.By using scp command we can transfer file from winscp to local as well as one folder in to another folder in winscp.
scp username#atechguideserver1.com:/data/tmp/samplscp.txt username#atechguideserver2.com:/data/tmp/dir/tech
Please refer to: http://99students.com/move-files-in-unix/

Related

WinSCP batch transfer with SQL Server Agent Job

I have a .bat file that:
executes WinSCP (calls a script txt) and transfers a file through SFTP. It also archives the file that was transferred.
My Problem is it works just fine when I double click the .bat file. BUT when I execute it in SQL Server agent job, it does archive the file but is not transffering the to the SFTP.
I get no error at all in the history of the job.
I feel it isnt executing the WinSCP or not opening the txt script. I checked permissions and it shows fine in all files and folders...
Checked FULL control for all users on all files and related folder.
Tried checking my syntax.
"C:\Program Files\WinSCP\WinSCP.exe" /command "option confirm off"
/console /script="C:\Program Files\WinSCP\LocalToRemote.txt"
You should add /log=C:\path\to\winscp.log switch to WinSCP command line to see what's going on.
In a rare case the error is so fatal that even no log file is produced, you should capture WinSCP console output. You cannot do that with winscp.exe, you need winscp.com for that, and you should use it anyway.
"C:\Program Files\WinSCP\WinSCP.com" ^
/log=C:\path\to\winscp.log ^
/command "option confirm off" ^
/script="C:\Program Files\WinSCP\LocalToRemote.txt" > C:\path\to\winscp.out
Until you show us the logs, I can only point you to WinSCP FAQ:
My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
The most usual problem is the first one listed in the FAQ:
The script fails (or “hangs”), because the host key (SFTP or SCP) or certificate (FTPS, WebDAVS) is not known by the service’s account, and WinSCP fails (or asks for) its verification.
See also Debugging transfer task running in Windows Scheduler, SSIS, or another automation service.
The best what you can do it to have WinSCP GUI generate script/command-line/batch-file template for you, with all details needed for the script to be really portable.

How to check for directory existence before creating it on remote server? [duplicate]

Trying to automate via Jenkins: Transfer files from Local Windows directory to FTP directory via WinSCP console scripting.
Problem statement: Need to check whether a folder exists on FTP server before initiating upload.
This is the code I'm trying to execute via winscp /script=abc.txt
abc.txt as follows:
open ftp://xyz
mkdir /Server_folder/test_folder/ABC
put FROM_LOCAL_PATH TO_SERVER_PATH
bye
Since you clarified you are using WinSCP scripting console
option batch continue
mkdir some_dir
option batch abort

Using FTP.exe to get unknown contents from a directory [duplicate]

I want to create a batch file in Windows Server, including the following functions:
Connection to a FTP server
Copying the files from there (directory called "out") to a local directory
if success, then deleting the files from the FTP server
repeating those steps every 15 minutes
I haven't done that much with batch files so far, so it would be great if you could help me. I know there is the ftp command, and I know how to connect (ftp open), but unfortunately I don't know how to copy those files from there every 15 minutes.
Thanks a lot for your help!
To program ftp from a batch file, see http://support.microsoft.com/kb/96269. You need to call ftp like this
ftp -i -s:ftpcommands.txt
where ftpcommands.txt looks something like this:
open ftp.myftpsite.com
username
password
bin
cd out
mget *
del *
bye
For running this every 15 minutes, see other replies (at or Command Scheduler).
(The -i parameter is to turn off interactive prompting - the other way to do this is to add a prompt off command to the commands text file before the mget. Without this, mget will stop and ask you to confirm before getting each file. [Thanks to Adriano for pointing this out!])
The accepted answer by #AAT suggests using Windows built-in ftp.exe command-line client. While that can work, more often it won't, because this client does support FTP active mode only, which does not play nicely with today's ubiquitous firewalls and NATs. It also does not support encrypted FTPS (FTP over TLS/SSL).
If you have a problem with the above, you need to use a 3rd party FTP client. Most of them do support both the passive mode and encryption.
For example with WinSCP FTP client, you can use the following batch file (.bat):
WinSCP.com /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /out/* c:\local\path\" ^
"exit"
In case you already have an ftp.exe script, there's a guide for converting it to WinSCP script.
For the scheduling part, see the guide to scheduling transfers to FTP server.
(I'm the author of WinSCP)
Windows has the at utility as well as the Windows task scheduler. Either one can run your program at a specified interval.
Using only one (1) .bat file script. Create the FTP script in a temporary file, run it, then delete the temporary file.
SET "FTPFILE=%TEMP%\myftpscript_%RANDOM%.txt"
ECHO>>"%FTPFILE%" open ftp.myftpsite.com
ECHO>>"%FTPFILE%" username
ECHO>>"%FTPFILE%" password
ECHO>>"%FTPFILE%" bin
ECHO>>"%FTPFILE%" cd out
ECHO>>"%FTPFILE%" mget *
ECHO>>"%FTPFILE%" del *
ECHO>>"%FTPFILE%" bye
ftp -i -s:"%FTPFILE%"
IF EXIST "%FTPFILE%" (DEL "%FTPFILE%")
EXIT /B 0

CMD, connect to SFTP with cmd command and copy file from there [duplicate]

I have program in Unix that generates files 8 files at 8:30 pm everyday.
I need to download the files into my Windows machine after the files are generated using WinSCP.
I can drag and drop those but its time consuming, I need to automate this process.
I cannot use .Net assembly to download those.
I have tried to use the get command, but its throwing error: Not an SCCS file.
Let me know how can I solve this.
--Thanks
To automate a task using WinSCP, use its scripting interface from a batch file (e.g. download.bat).
I assume you want to use SFTP, as you are connecting to a *nix server.
The simplest download batch file is like:
winscp.com /log=c:\path\to\log\winscp.log /command ^
"open sftp://username:password#example.com/ -hostkey=""xxx""" ^
"get /path/to/file.ext c:\path\to\download\to\" ^
"exit"
Replace the username, password and example.com with the actual connection details. Get the value of -hostkey switch from your GUI session. Use real paths.
Though it's easier to have WinSCP generate the batch file for you.
For details see a guide to automating file transfers from SFTP server using WinSCP.
Once you have the batch file working, schedule the the batch file execution using Windows scheduler.
See a guide to scheduling file transfers from SFTP server.

Getting back a file sent from remote PC using BATCH with PsExec C#

I am coding a program in C# to communicate with a remote PC in an Wifi AdHoc network. I execute a BATCH file in the remote that will send to the local a CHECK.txt file. I use PsExec.
Everything works fine in my C# program when I execute this batch file remotely with PsExec from the local PC to copy the CHECK.txt file in any of the remote's directories. The problem comes when I modify this BATCH to copy the text file to the LOCAL:
copy C:\Windows\CHECK.txt \\192.168.1.10\C$\Windows
It seems that the process PsExec, used to execute the BATCH in remote, blocks the communication of the network when the BATCH tries to send back to the local the text file. Ports 445 and 139 problem? Any idea of what is blocking the file to be sent back?
Everything is set for a transparent dialog between remote and local (no firewall, etc).
Thanks in advance...
I just noticed the -s parameter on your PSEXEC command. The -s means Run the remote process in the System account.. Removing it should allow your batch script to write back to local computer.

Resources