简体   繁体   English

FTP将所有文件从目录复制到本地目录

[英]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\\". 我正在尝试创建一个批处理文件,使用用户名mark123和密码testabc将FTPs FTPs到URL(例如ftp.tester123.com.au)的目录“ C:\\ Documents \\ Client \\”到本地目录“ C:\\ Desktop \\ GoHere \\”。

The script is to copy all the files within this ftp directory to my local machine. 该脚本是将此ftp目录中的所有文件复制到本地计算机。 I have read the following msdn tutorial https://support.microsoft.com/en-gb/kb/96269 我已经阅读了以下msdn教程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? winscp可以让我执行此任务并为其创建一个批处理文件吗? I would like to automate execution of this directory - at say midnight every night. 我想自动执行此目录-每天晚上12点。 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. 另外,我对.NET非常熟悉,并且winSCP具有包装程序,可让您编写C#而不是标准脚本。 If I go with a .NET approach, can I create a simple .exe or batch file that can simply be executed by double clicking? 如果我采用.NET方法,是否可以创建一个可以简单地通过双击执行的简单.exe或批处理文件?

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 : 最简单的方法是利用WinSCP功能生成传输代码

  • Login to your FTP server with WinSCP; 使用WinSCP登录到您的FTP服务器;
  • 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 : 在这里,您可以选择是否要WinSCP 生成WinSCP脚本,甚至是完整的Windows批处理文件

在此处输入图片说明

or .NET assembly (C#) code : .NET汇编(C#)代码

在此处输入图片说明

(These are official screenshots from WinSCP documentation. So they show SFTP upload, not FTP download) (这些是WinSCP文档的官方屏幕截图。因此,它们显示的是SFTP上传,而不是FTP下载)


Now that you have your batch file or C# executable ready, you can schedule it to be run using Windows Scheduler . 现在您已经准备好批处理文件或C#可执行文件,您可以使用Windows Scheduler安排其运行

If you have WinSCP installed, then the following code will work for you. 如果您已安装WinSCP,则以下代码将为您工作。 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. 您也可以使用WinSCP生成此代码。

@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. 根据需要对其进行修改,然后将代码放入.bat文件中。 Later you can execute it manually or use Windows Task Scheduler. 以后,您可以手动执行它或使用Windows Task Scheduler。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM