从FTP服务器获取文件并将其复制到UNC目录 [英] Get file from FTP server and copy it to UNC directory

查看:507
本文介绍了从FTP服务器获取文件并将其复制到UNC目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码:

pre > for / fdelims =%% x in(config.bat)do(set%% x)
echo Start [%time%]>> %primefolder%\TimeRun.log
REM ----------------------------------- -------------------------------------------------- -------
REM从ftp服务器导入.csv - 将csv导入到本地目录
REM Ftpcmd.txt连接到ftp服务器并将.csv文件复制到本地目录
REM ----------------------------------------------- ---------------------------------------------
回声打开%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呼叫并执行FTP命令文本document(ftpcmd.txt)
REM此代码调用建立连接的文件,然后将文件复制到本地目录
REM创建并填充动态FTP命令文件。
REM --------------------------------------------- -------------------------------------------------- ------------------------------
ftp -i -s:%primefolder%\ftpcmd.txt >%primefolder%\logs\ftpnport.log2>>%primefolder%\logs\ftperr.log
echo [%date% - %time%]>>> %primefolder%\logs\ftpinport.log
ftp -i -d -s:%primefolder%\ftpcmd.txt
for / ftokens = *%% a in %primefolder%\logs\ftperr.log)(echo [%date% - %time%] [错误级别:1] [问题位置:FTP] [错误:%% 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

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




> mget 更改为 ,如 @Martin Prikryl的回答所示,我得到这个错误:


R:I / O错误

这是ftp输出:

  ftp>开放111.11.1.1 
与111.11.1.1连接。
220欢迎使用Code-Crafters Ability FTP Server。
用户(111.11.1.1:(无)):
331请立即发送PASS。

230 - 欢迎使用用户。
230 - 当前有1个用户登录到此服务器。
230 - 目前有1个用户登录该帐户。
230 - 您拥有剩余的无限KB帐户分配。
230 - 您剩余0个转帐余额。
230 - 每下载一个KB,您将损失0个积分。
230 - 您上传的每KB上传失去0个积分。
230您目前位于目录/中。
ftp> cd \CSV\
250/ CSV是当前目录。
ftp>二进制
200类型设置为'我'(图像)。
ftp>获取User.csv \\test.org\test_this\Implementation\New Projects\New\Interface\User.csv
200 PORT命令成功。
150建立数据连接,开始传输。
226转账完成。
ftp:以2.64秒接收到的1277532字节483.91千字节/秒。
ftp>断开
221感谢您的访问。
ftp>退出






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

  ftp>开放111.11.1.1 
连接到111.11.1.1
220欢迎使用Code-Crafters Ability FTP Server。
用户(111.11.1.1:(无)):
331请立即发送PASS。

230 - 欢迎使用用户。
230 - 当前有100个用户中有2个用户登录到此服务器。
230 - 目前有1个用户登录该帐户。
230 - 您拥有剩余的无限KB帐户分配。
230 - 您剩余0个转帐余额。
230 - 每下载一个KB,您将损失0个积分。
230 - 您上传的每KB上传失去0个积分。
230您目前位于目录/中。
ftp> cd \CSV\
250/ CSV是当前目录。
ftp>二进制
200类型设置为'我'(图像)。
ftp>获取User.csv \\test.org\test_this\Implementation\New Projects\New\Interface\User.csv
200 PORT命令成功。
150建立数据连接,开始传输。
> R:I / O错误
226传输完成。
ftp:在2.84秒中接收的1277532字节449.20千字节/秒。
ftp>断开
221感谢您的访问。
ftp>退出


解决方案

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



因此 lcd \\example.com\share 不起作用。



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

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



所以你可以使用 get 命令,它允许你指定一个目标路径。

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

另请注意,您的%offload %路径包含一个空格( New Projects ),您需要将路径括在双引号中。



最后删除

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

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






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



例如,您可以使用 WinSCP脚本

  winscp.com /log=winscp.log/命令^ 
打开ftp://%FTPUsername%:%FTPPassword%@% FTPIP%/^
cd%FTPPrimary%^
get%Filename%%offload%\^
exit> %primefolder%\logs\ftpinport.log

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

参考文献:



(我是WinSCP的作者)


I am trying to copy a CSV file from an FTP server to a UNC file path. I had it working from FTP to a local path, but it would be better for my project to have it copy to a UNC path. This must all be achieved in batch and FTP commands.

This is my code so far:

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")

This is the config file:

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

Thank you for all your help ahead of time!


After changing mget to get as suggested in an answer by @Martin Prikryl, I am getting this error:

R:I/O Error

This is the ftp output:

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


When redirecting the error and output of ftp to one file I get this as the output:

 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

解决方案

UNC path cannot be a working directory in Windows.

So lcd \\example.com\share won't work.

The mget command does not allow your to specify a target path.

But you do not seem to need the mget, as you are not using a wildcard, but downloading a specific file.

So you can use the get command instead, which allows you to specify a target path.

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

Also note that as your %offload% path contains a space (New Projects) you need to enclose the path to double-quotes.

And finally remove the

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

Side note: There's no /D switch to the FTP lcd command (there are no switches in the Windows ftp.exe whatsoever).


Had you need to use the wildcard, you would have to use a different FTP client.

For example with WinSCP scripting you can use:

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

Note that the WinSCP get command supports wildcards and allows you to specify target path at the same time. WinSCP also defaults to binary transfer mode.

References:

(I'm the author of WinSCP)

这篇关于从FTP服务器获取文件并将其复制到UNC目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆