【问题标题】:Get file from FTP server and copy it to UNC directory从 FTP 服务器获取文件并将其复制到 UNC 目录
【发布时间】:2015-09-25 13:45:36
【问题描述】:

我正在尝试将 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%\logs\ftpinport.log" 2>>"%primefolder%\logs\ftperr.log"
echo[%date% - %time%] >> "%primefolder%\logs\ftpinport.log" 
ftp -i -d -s:%primefolder%\ftpcmd.txt
for /f "tokens=*" %%a in (%primefolder%\logs\ftperr.log) do (echo [%date% - %time%]  [Error Level: 1][Issue Location:FTP][Error:%%a] >> "%primefolder%\logs\error.log")

这是配置文件:

primefolder=C:\scripts
FTPIP=111.11.1.1
FTPUsername=User
FTPPassword=test
Filename=User.csv
FTPPrimary=\CSV\
FTPArchive=\CSV\Archive
offload=\\test.org\test_this\Implementation\New Projects\New\Interface

感谢您提前提供的所有帮助!


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

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\test_this\Implementation\New Projects\New\Interface\User.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\test_this\Implementation\New Projects\New\Interface\User.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

【问题讨论】:

    标签: batch-file ftp unc


    【解决方案1】:

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

    所以lcd \\example.com\share 不起作用。

    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%\logs\ftpinport.log" 
    

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

    参考资料:

    (我是 WinSCP 的作者)

    【讨论】:

    • 感谢您的帮助。使用 get 命令时,我知道会收到此错误:“I/O 错误”
    • 我已经用您要求的信息更新了原始问题
    • ftp -i -s:"%primefolder%\ftpcmd.txt" >"%primefolder%\logs\ftpimport.log" 2>>"%primefolder%\logs\ftperr.log" 拆分将其转换为输出和错误日志。在错误日志中我得到 I/O 错误
    • 完美!非常感谢。
    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多