2

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!

Mark
  • 501
  • 2
  • 11
  • 28
  • What specific problem do you have? What do you need in addition to the batch file shown in my answer to your previous question [Copy all files from one FTP directory to another](http://stackoverflow.com/a/39384635/850848)? – Martin Prikryl Sep 08 '16 at 09:35
  • What does it mean *"FTPs into a URL ... **with directory "C:\Documents\Client\" .. into local directory "C:\Desktop\GoHere\"**.* – Martin Prikryl Sep 08 '16 at 09:37
  • I mean the host name is ftp.tester123.com.au. Once a connection is established to that host I want to copy all the files from host directory "C:\Documents\Client\" to my local machine into my local directory "C:\Desktop\GoHere\". – Mark Sep 08 '16 at 09:50
  • You cannot use local (as of the host) paths like `C:\Documents\Client` in FTP. The path has to be made accessible by the FTP server and mapped to an FTP path. And you have to use that FTP path. See [How to form FTP URL using absolute path in windows?](http://superuser.com/q/1103547/213663) – Martin Prikryl Sep 08 '16 at 09:53
  • Okay, and what about once I have mapped the FTP paths, how can I create the batch file with the copying process inside? Is this something that can be done via the GUI inside WinSCP or using the .NET wrapper? – Mark Sep 08 '16 at 10:06
  • I have a Filezilla server where I map usernames and passwords to specific directories. Is this the same as mapping an FTP path with winSCP like you mentioned above? – Mark Sep 08 '16 at 10:19

2 Answers2

2

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:

enter image description here

or .NET assembly (C#) code:

enter image description here

(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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank you so much for your help, this is a fantastic answer Martin. However I do have one question. I've copied and paste the generated code into a .bat file and for some reason when I run the .bat file through command line I get an error with the first line saying: "C:\Desktop\GoHere" is not recognized as an internal or external command, operatable program or batch file. Error. What am I doing wrong here? Am I executing the batch incorrectly or something else? – Mark Sep 08 '16 at 11:47
  • Show us the batch file. – Martin Prikryl Sep 08 '16 at 11:54
  • @echo off "C:\Users\mark\Desktop\New folder (3)\WinSCP.com" ^ /log="C:\writable\path\to\log\WinSCP.log" /ini=nul ^ /command ^ "open ftp://test:tester%%24@ftp.tester123.com.au/" ^ "cd /" ^ "lcd ""C:\Users\mkirilow\Documents\New folder (3)""" ^ "get *" ^ "exit" set WINSCP_RESULT=%ERRORLEVEL% if %WINSCP_RESULT% equ 0 ( echo Success ) else ( echo Error ) exit /b %WINSCP_RESULT% ^This is the contents of my batch file. Filename :tester.bat – Mark Sep 08 '16 at 12:22
  • There's no `C:\Desktop\GoHere` in the batch file. So I have no idea what you are trying to do. Show us a screenshot. – Martin Prikryl Sep 08 '16 at 12:25
  • The GoHere directory was just a sample directory, and not my actual directory to use. The above directories in the code are the ones I want to use (New folder(3) has been mapped using filezilla server). I'm thinking that FileZilla's FTP mapping could be what is causing the issue. Aside from the directories, was the syntax and method used to execute the batch correct? – Mark Sep 08 '16 at 12:43
  • Your comment does not make any sense. In the batch fie, the `New folder(3)` is a local path. It has nothing to do with FileZilla FTP server. + *"syntax and method used to execute the batch correct"* - What you posted **is a batch file**. I do not know how you **execute** it, so I cannot answer that. – Martin Prikryl Sep 08 '16 at 12:47
  • I've changed my approach to this problem to use the .NET wrapper. I am able to establish a connection to the ftp with session.Open() however I get a SessionRemoteException, with an innerException stating '{"Can't open file 'C:\\Users\\mark\\Documents\\tester\\AK2H2016072512361.XML'.\r\nCopying files from remote side failed."}' The getFiles implementation I am using is the following: session.GetFiles("/*", @"C:\Users\mark\Documents\tester\"); What am I doing wrong here? – Mark Sep 09 '16 at 02:45
  • Does the directory `C:\Users\mark\Documents\tester` exist? – Martin Prikryl Sep 09 '16 at 05:42
1

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.

Plabon Dutta
  • 6,819
  • 3
  • 29
  • 33
  • Note that WinSCP has [`%TIMESTAMP%` syntax](https://winscp.net/eng/docs/scripting#timestamp), which you can use instead of unreliable `%date%`: `"cd /export/home/sysm/opt/oss/server/var/fileint/pm/pmexport_%%TIMESTAMP#yyyymmdd%%" ^` – Martin Prikryl Jun 12 '19 at 10:45
  • I didn't know that. Thanks a lot for mentioning. – Plabon Dutta Jun 12 '19 at 11:06