从FTP服务器获取文件并将其复制到UNC目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从FTP服务器获取文件并将其复制到UNC目录相关的知识,希望对你有一定的参考价值。

我试图将CSV文件从FTP服务器复制到UNC文件路径。我有它从FTP到本地路径,但我的项目将它复制到UNC路径会更好。这必须在批处理和FTP命令中实现。

到目前为止这是我的代码:

for /f "delims=" %%x in (config.bat) do (set "%%x")
echo Start[%time%] >> "%primefolder%TimeRun.log"
REM --------------------------------------------------------------------------------------------
REM  Import .csv from the ftp server - csv imported to local directory
REM         Ftpcmd.txt connects to the ftp server and copies the .csv file to a local directory
REM --------------------------------------------------------------------------------------------
echo open %FTPIP%> %primefolder%ftpcmd.txt
echo %FTPUsername%>> %primefolder%ftpcmd.txt
echo %FTPPassword%>> %primefolder%ftpcmd.txt
echo cd %FTPPrimary%>> %primefolder%ftpcmd.txt
echo binary>> %primefolder%ftpcmd.txt
echo lcd /D "%offload%" >> %primefolder%ftpcmd.txt
echo mget %Filename%>> %primefolder%ftpcmd.txt
echo disconnect>> %primefolder%ftpcmd.txt
echo quit>> %primefolder%ftpcmd.txt
REM -----------------------------------------------------------------------------------------------------------------------------
REM  Call and execute the FTP command text document (ftpcmd.txt)
REM         This Code calls the file which establishes a connection and then copies the file to a local directory   
REM         Dynamic FTP command file is created and populated.
REM -----------------------------------------------------------------------------------------------------------------------------
ftp -i -s:"%primefolder%ftpcmd.txt" >"%primefolder%logsftpinport.log" 2>>"%primefolder%logsftperr.log"
echo[%date% - %time%] >> "%primefolder%logsftpinport.log" 
ftp -i -d -s:%primefolder%ftpcmd.txt
for /f "tokens=*" %%a in (%primefolder%logsftperr.log) do (echo [%date% - %time%]  [Error Level: 1][Issue Location:FTP][Error:%%a] >> "%primefolder%logserror.log")

这是配置文件:

primefolder=C:scripts
FTPIP=111.11.1.1
FTPUsername=User
FTPPassword=test
Filename=User.csv
FTPPrimary=CSV
FTPArchive=CSVArchive
offload=\test.org	est_thisImplementationNew ProjectsNewInterface

提前感谢您的帮助!


按照mget的建议将get更改为an answer by @Martin Prikryl后,我收到此错误:

R:I / O错误

这是ftp输出:

ftp> open 111.11.1.1
Connected to 111.11.1.1.
220 Welcome to Code-Crafters Ability FTP Server.
User (111.11.1.1:(none)): 
331 Please send PASS now.

230-Welcome "User".
230-There are currently 1 of 100 users logged onto this server.
230-There are currently 1 users logged onto this account.
230-You have unlimited KB of account allocation left.
230-You have 0 transfer credits remaining.
230-You lose 0 credits per KB downloaded.
230-You lose 0 credits per KB uploaded.
230 You are currently in directory "/".
ftp> cd CSV
250 "/CSV" is current directory.
ftp> binary
200 Type set to 'I' (IMAGE).
ftp> get User.csv \test.org	est_thisImplementationNew ProjectsNewInterfaceUser.csv 
200 PORT command successful.
150 Data connection established, beginning transfer.
226 Transfer complete.
ftp: 1277532 bytes received in 2.64Seconds 483.91Kbytes/sec.
ftp> disconnect
221 Thanks for visiting.
ftp> quit

将错误和输出ftp重定向到一个文件时,我将其作为输出:

 ftp> open 111.11.1.1
    Connected to 111.11.1.1
    220 Welcome to Code-Crafters Ability FTP Server.
    User (111.11.1.1:(none)): 
    331 Please send PASS now.

    230-Welcome "User".
    230-There are currently 2 of 100 users logged onto this server.
    230-There are currently 1 users logged onto this account.
    230-You have unlimited KB of account allocation left.
    230-You have 0 transfer credits remaining.
    230-You lose 0 credits per KB downloaded.
    230-You lose 0 credits per KB uploaded.
    230 You are currently in directory "/".
    ftp> cd CSV
    250 "/CSV" is current directory.
    ftp> binary
    200 Type set to 'I' (IMAGE).
    ftp> get User.csv \test.org	est_thisImplementationNew ProjectsNewInterfaceUser.csv  
    200 PORT command successful.
    150 Data connection established, beginning transfer.
    > R:I/O Error
    226 Transfer complete.
    ftp: 1277532 bytes received in 2.84Seconds 449.20Kbytes/sec.
    ftp> disconnect
    221 Thanks for visiting.
    ftp> quit
答案

UNC路径不能是Windows中的工作目录。

所以lcd \example.comshare将无法运作。

mget command不允许您指定目标路径。

但是您似乎不需要mget,因为您没有使用通配符,而是下载特定文件。

因此,您可以使用get command,它允许您指定目标路径。

echo get %Filename% "%offload%\%Filename%" >> %primefolder%ftpcmd.txt

另请注意,由于%offload%路径包含空格(New Projects),因此需要将路径括在双引号中。

最后删除

echo lcd /D "%offload%" >> %primefolder%ftpcmd.txt

旁注:没有/D切换到FTP lcd command(Windows ftp.exe中没有任何开关)。


如果您需要使用通配符,则必须使用其他FTP客户端。

例如,使用WinSCP scripting,您可以使用:

winscp.com /log=winscp.log /command ^
    "open ftp://%FTPUsername%:%FTPPassword%@%FTPIP%/" ^
    "cd %FTPPrimary%" ^
    "get %Filename% %offload%" ^
    "exit" > "%primefolder%logsftpinport.log" 

请注意,WinSCP get command支持通配符,并允许您同时指定目标路径。 WinSCP也默认为二进制传输模式。

参考文献:

(我是WinSCP的作者)

以上是关于从FTP服务器获取文件并将其复制到UNC目录的主要内容,如果未能解决你的问题,请参考以下文章

java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?

在 PHP 中创建文件并将其上传到 FTP 服务器而不在本地保存

如何获取文本文件的内容并将其复制到剪贴板?

我可以创建从网络上的UNC路径复制文件的安装程序吗?

如何在不保存文件的情况下制作打印屏幕并将其发送到 FTP 服务器?我的工作代码将文件保存到硬盘

MFC如何从MFCEditBrowseControl获取路径并将其复制到编辑控件?