Get file from FTP server and copy it to UNC directory

I am trying to copy a CSV file from an FTP server to a UNC file path. I've worked with FTP on a local path, but I'd rather have my project copied to a UNC path. All of this should be achieved in batch and FTP commands.

This is my code:

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 your help in advance!


After changing mget

to get

as suggested in @ Martin Prikryl's answer , I get 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 ftp output to one file, I get this as 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

      

+3


source to share


1 answer


UNC path cannot be the working directory on Windows.

So that lcd \\example.com\share

won't work.

The command does not allow you to specify the target path. mget

But you don't seem to need mget

it as you are not using a wildcard, but rather downloading a specific file.

So you can use get

command
, which allows you to specify the target path.

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

      

Also note that since your path %offload%

contains a space ( New Projects

), you need to enclose the path in double quotes.

Finally, remove

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

      



Side note: no /D

switch to FTP lcd

command
(on Windows whatsoever).


If you need to use a wildcard, you will have to use a different FTP client.

For example, using WinSCP scripts , 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 WinSCPget

command supports wildcards and allows you to specify the target path at the same time. WinSCP also uses binary transfer mode by default.

Literature:

(I am the author of WinSCP)

+1


source







All Articles