New to batch files, need to automate ftp download.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
imdabaum
Posts: 4
Joined: 02 Jul 2010 15:26

New to batch files, need to automate ftp download.

#1 Post by imdabaum » 02 Jul 2010 15:56

I have been assigned a task to automate an ftp download each morning. The file name stays the same except for a date suffix that is appended on the end of it.

I have the username and password and ftp site, but nothing I've found seems to allow me to connect. I've written one batch files to synchronize network drives and my hard drive for the purpose of backing up my material. So needless to say, I'm very new to batch scripting. Someone told me I needed the IP address, but I've seen multiple examples of batch files that don't use the IP. I would appreciate any direction.

open ftp.site.com
user {username}
{password]
prompt
mget File_20100702.csv
bye

Also the file changes every day, FileName_{Today, Format("yyyymmdd")}
If anyone could explain how I might put that as a variable or something so I don't have to edit the batch file every day, that would be great as well.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New to batch files, need to automate ftp download.

#2 Post by aGerman » 02 Jul 2010 16:39

I'm not that familar with FTP, but this is doable.
First have a look at these examples.

To assemble the date to the right format I need to know what an
echo %date%
would display on your computer.

Regards
aGerman

imdabaum
Posts: 4
Joined: 02 Jul 2010 15:26

Re: New to batch files, need to automate ftp download.

#3 Post by imdabaum » 08 Jul 2010 12:05

ECHO %date% gives me DayOfWeek(Three letters) mm/dd/yyyy.

Thanks.

Also I had checked out that link and is where I came up with the model I am working with.

Code: Select all

open example.com
username
password
!:--- FTP commands below here ---
lcd I:\ImportantFiles\Brandon
mget "{Filename}*"
disconnect
bye

I realize I'm going to get more files than I want at the moment, but I wanted to see if it would work.
It didn't.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New to batch files, need to automate ftp download.

#4 Post by aGerman » 08 Jul 2010 13:39

A FTP file is no batch file. You could write and call it by batch.

Try something like that:

Code: Select all

@echo off &setlocal

for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set "dateStamp=%%c%%a%%b"

set "ftpFile=%temp%\get.ftp"

>"%ftpFile%" echo.open example.com
>>"%ftpFile%" echo.username
>>"%ftpFile%" echo.password
>>"%ftpFile%" echo.lcd I:\ImportantFiles\Brandon
>>"%ftpFile%" echo.mget File_%dateStamp%.csv
>>"%ftpFile%" echo.disconnect
>>"%ftpFile%" echo.bye

ftp -v -i -s:"%ftpFile%"

del "%ftpFile%"
pause



Regards
aGerman

imdabaum
Posts: 4
Joined: 02 Jul 2010 15:26

Re: New to batch files, need to automate ftp download.

#5 Post by imdabaum » 08 Jul 2010 15:00

It says unknown host..

I use the same path in IE and it works, but if I try it through this it doesn't recognize it. Any suggestions?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New to batch files, need to automate ftp download.

#6 Post by aGerman » 08 Jul 2010 15:11

Is your file placed directly on the host or on a share?

Say your file is placed on //ftp:ftp.site.com/share/ you have to use the "cd" command. "open" is only for the host.


Code: Select all

@echo off &setlocal

for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set "dateStamp=%%c%%a%%b"

set "ftpFile=%temp%\get.ftp"

>"%ftpFile%" echo.open ftp.site.com
>>"%ftpFile%" echo.username
>>"%ftpFile%" echo.password
>>"%ftpFile%" echo.lcd I:\ImportantFiles\Brandon
>>"%ftpFile%" echo.cd share
>>"%ftpFile%" echo.mget File_%dateStamp%.csv
>>"%ftpFile%" echo.disconnect
>>"%ftpFile%" echo.bye

ftp -v -i -s:"%ftpFile%"

del "%ftpFile%"
pause



Regards
aGerman

imdabaum
Posts: 4
Joined: 02 Jul 2010 15:26

Re: New to batch files, need to automate ftp download.

#7 Post by imdabaum » 08 Jul 2010 17:25

So if I type in ftp:username:password@ftp.site.com in my browser, it takes me directly to the place I need to download the file.

Are you saying that instead of using the open ftp.site.com command, I need to do something like this?

@echo off &setlocal

for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set "dateStamp=%%c%%a%%b"

set "ftpFile=%temp%\get.ftp"

>"%ftpFile%" echo.cd ftp.site.com
>>"%ftpFile%" echo.username
>>"%ftpFile%" echo.password
>>"%ftpFile%" echo.lcd I:\ImportantFiles\Brandon
>>"%ftpFile%" echo.mget File_%dateStamp%.csv
>>"%ftpFile%" echo.disconnect
>>"%ftpFile%" echo.bye

ftp -v -i -s:"%ftpFile%"

del "%ftpFile%"
pause

That just tells me that the host has not connected. Sorry for the troubles. I am confused.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: New to batch files, need to automate ftp download.

#8 Post by aGerman » 08 Jul 2010 17:39

No. You need the "open" command. But if the file is placed in a sub folder you must not write the entire path behind the "open" command. Once more, I wrote:
Say your file is placed on //ftp:ftp.site.com/share/...

...
>"%ftpFile%" echo.open ftp.site.com

...
>>"%ftpFile%" echo.cd share


So the question was if the file is placed directly on "ftp.site.com" or is it placed in a subfolder.

Hope you will understand, what I try to explain.

Regards
aGerman

Post Reply