Download files from Unix server using WinSCP with get command - windows

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.

Related

FTP copy all files from a directory to local directory

I am trying to create a batch file that FTPs into a URL(say ftp.tester123.com.au) with directory "C:\Documents\Client\" using username mark123 and password testabc into local directory "C:\Desktop\GoHere\".
The script is to copy all the files within this ftp directory to my local machine. I have read the following msdn tutorial
https://support.microsoft.com/en-gb/kb/96269
But it didn't help me with my current issue. Will winscp allow me to perform this task and create a batch file for it? I would like to automate execution of this directory - at say midnight every night. Is this possible?
Alternatively I am very familiar with .NET and winSCP has a wrapper to allow you to write C# instead of standard scripting. If I go with a .NET approach, can I create a simple .exe or batch file that can simply be executed by double clicking?
Thanks in advance!
What you are trying to do is a very trivial task. What you are missing is a conceptual understanding of the task. So it's difficult to give you an answer that will help you, as it's difficult to understand what piece of knowledge you are missing to accomplish it.
The easiest approach to start with, is to make use of WinSCP ability to generate a transfer code:
Login to your FTP server with WinSCP;
Navigate to the source remote directory and to the destination local directory;
Select the remote files to download (all files?);
Use the Files > Download command in the main menu (or the Download command in the files' context menu);
On the Download dialog, click the drop down arrow on the Transfer Settings button and select the Generate Code command.
You will get the Generate transfer code dialog.
There, you can choose if you want WinSCP to generate a WinSCP script or even complete Windows batch file:
or .NET assembly (C#) code:
(These are official screenshots from WinSCP documentation. So they show SFTP upload, not FTP download)
Now that you have your batch file or C# executable ready, you can schedule it to be run using Windows Scheduler.
If you have WinSCP installed, then the following code will work for you. I fetched all files from a particular folder and downloaded it to a local folder. Wrote a logfile along the way. You can generate this code using WinSCP too.
#echo off
set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
set d_name=/export/home/sysm/opt/oss/server/var/fileint/pm/pmexport_%mydate%
#echo %d_name%
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\HP\Desktop\Datacom\Reports\WinSCP.log" /ini=nul ^
/command ^
"open sftp://YOUR_USERNAME:YOUR_PASSWORD#FTP_SERVER_IP/ -hostkey=""ssh-rsa 1024 PUuYRVADKXB9j1Si+o89v2fsrsr7w2ZrV3NIqdz6kus0GtY=""" ^
"cd %d_name%" ^
"lcd C:\Users\HP\Desktop\Datacom\Reports\Test_ftp" ^
"get *" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
Modfify it according to your need and then put the code into a .bat file. Later you can execute it manually or use Windows Task Scheduler.

Downloading files with defined changed date time

Dears, could you please share your ideas how to download files from an FTP server to a local directory filtered by changed date.
So, I have a folder on the FTP server with files, but I need to move just those of them which have been changed yesterday (f.e.).
Thanks in advance for your reply.
open anymail.com
login
pass
!:--- FTP commands below here ---
lcd D:\Test
cd /home/my_folder
binary
mget *
disconnect
bye
You can hardly do this with the Windows built-in ftp.exe.
You would have to list the remote directory, redirect the listing to a file. Then parse the listing file to find the files you want. And then generate an ad-hoc download script. Quite a lot of work.
Use some more capable 3rd party Windows FTP command-line client.
For example with WinSCP scripting you can use a batch file (e.g. download_yesterdays.bat) like:
winscp.com /ini=nul /log=ftp.log /command ^
"open ftp://user:password#ftp.example.com/" ^
"cd /remote/path" ^
"lcd c:\local\path" ^
"get *>=yesterday<today" ^
"exit"
The >=yesterday selects files created since yesterday (inclusive). The <today excludes files created today. This syntax is supported since WinSCP 5.15.
In earlier versions of WinSCP, you can use %TIMESTAMP% syntax instead: >=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd% (the TIMESTAMP-1D syntax is supported since WinSCP 5.9).
References:
WinSCP guide to Downloading the most recent file from FTP server
File masks with time-constraints
%TIMESTAMP% syntax
(I'm the author of WinSCP)

Daily upload of file automation using batch script and WinSCP

So I do have a file that I generate weekly from a server using crontab in Linux side and transfer it to my PC. However, I am having a problem when try to send the file that I generate from a different server on Windows side using task scheduler.
Your command-line syntax is wrong.
I'm assuming the \ftpBinverlog_%yyyy%-%mm%-%dd%.txt is the file, you want to download.
It won't work, if you just specify it on command-line like you did.
Also neither Windows scheduler, nor command-interpreter, nor WinSCP understand syntax like %yyyy%.
The path to the remote file does not look good either. *nix systems use forward slashes, not backslashes.
So just keep your /script and /log arguments:
/script=C:\batchrun\Binver\script.tmp /log="C:\BIN VERIFICATION\ftplog"
And make sure the script.tmp looks like:
open sftp://user#example.com
get /ftpBinverlog_%TIMESTAMP#yyyy-mm-dd%.txt C:\target_path\
exit
References:
Guide to automating file transfer from SFTP server
%TIMESTAMP syntax.
Develop a batch file which will download/upload the required file using the SCP command
check this for more details.
check this for more details about the scp command parameters.
Make sure that you are able to run the batch with a successful result, then configure it within a scheduled task.
I hope this could help.

Implicit TLS FTP connection using WinSCP or other command line FTP client in Windows

I need to connect to an FTP secured with implicit TLS, username and password from the command line so that I can create a batch later on. I am using WinSCP since it's the most popular (I think) or the one that I can use to achieve this according to extensive googling. I'm not an expert but reading the documentation I have this so far with unsuccessful connection from command prompt:
winscp> open username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
Once connected I need to extract everything there to a folder \hostname\folder or c:\folder then delete the files on the ftp (more or less like cut)
Thanks to Martin Prikryl correction I was able to connect, here is what I did:
Created a text file with the winscp.com commands "connection.txt":
# Automatically abort script on errors
option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect with username and password
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa...."
# Change local directory
lcd C:\local_working_folder
# Force binary mode transfer
option transfer binary
# Download missing files to local directory from remote
synchronize local
# Delete all remote files
rm *
# Disconnect
close
# Exit WinSCP
exit
Now from command prompt or in a batch file you just call winscp.exe from where it is installed. If from installation media it should be C:\Program Files\WinSCP\WinSCP.exe of if extracted from a .zip portable version point to the folder where you extracted the file:
C:\WinSCP\WinSCP.exe /console /script="connection.txt" /log="conn.log"
Then just schedule the batch to automate the process.
Your syntax is almost correct. You are missing only the protocol prefix ftp:// (note that WinSCP defaults to the SFTP protocol).
open ftp://username:password#host:port -implicit -certificate="aa:aa:aa:aa..."
For details see:
https://winscp.net/eng/docs/session_url
https://winscp.net/eng/docs/scriptcommand_open
If that does not help, you need to share with us a script log file or at least an error message.

How to change remote file permission via FTPClient

I need to handle remote file on a ftp server via commons-net-1.4.1.jar,class org.apache.commons.net.ftp.FTPClient.It's seems that no api available to change a file's permission,just like unix shell command chmod.
Anyone know any solution to resolve this issue?
FTP doesn't have a command for such operations, since the operation is platform-specific. If your library supports this, you can try using SITE command and pass the command line as a parameter of SITE command. Some servers support this trick to run shell commands.

Resources