# Batch file needed to get file from ftp site



## gmazza

Hey there,
I am totally new to batch programs and ftp stuff. I have been searching and trying to read how to do this but I can't get anywhere.
I need a batch script to get a file from an ftp site copied onto my pc.
So far I have the name of the ftp site.
I typed in ipconfig in a DOS window, so is the DNS considered my host server name?
I just don't know where to start. 
I read how to do it but nothing is explained very well and doesn't relate to what I am doing.
Thanks for your help in advance!


----------



## dm01

Just use an FTP client, such as Filezilla [ http://filezilla-project.org/ ]. The way you're looking at is unnecessarily complicated.

Why do you want to only download the pages?


----------



## gmazza

I don't want to download the page. There is a .csv file on the ftp site, I want that file copied/transferred to a folder on my network, but I need this done through a batch job as it will run nightly.


----------



## ahmorrow

I have no experience in the area, but maybe this place does: http://www.robvanderwoude.com/ftp.php

Hopefully that helps, welcome to TSF


----------



## gmazza

Thanks for the referral but that link is like most I have run across. Very brief and not detailed. I need it spelled out way more than that :smile:

Thanks for the welcome, happy to be here!!


----------



## ahmorrow

Alright. I'll see if I can explain it better as I read through it.

ftp is known as the file transfer protocol, it's a way to access a web site's shell--file structure--like your own computer. The batch file--if stored and run from your own computer--won't need to know what your hostname or DNS name is.

For ftp all that you need is a username/password and the hostname of the ftp site. In addition you might want to specify a place where you want the file placed.

For instance:

User: gmazza
Pass: 123456
Host: ftp://www.myspace.com/
Dir: c:\Documen~\USERNAME\My Documents\

Here's a sample with notes. Does this help you at all?



Code:


#Start FTP and choose location
ftp ftp.hostname.com
#USER waits until prompted for username and inputs MyUserId
USER MyUserId
#Inputs MyPassword as password to login to ftp site.
MyPassword
#Changes Directory to files/pictures if it exists (within the ftp site)
cd files/pictures
#Set transfer type to binary--best to use this for anything really
binary
#Turn off prompting--aka unattended
prompt n
#Use this to get all files with the extension jpg
mget *.jpg
#Use this to get filename.jpg
get filename.jpg


----------



## TheOutcaste

There are 6 things you need:
1 FTP Server Name (FTP Site name, either the name or IP address
2 UserName for the FTP Account
3 Password for the FTP Account 
4 Local Folder, where on your PC you want to store the file
5 Remote Folder, where the file is on the FTP Server 
6 Filename, the name of the file to retrieve

The Remote folder path should be relative to the folder you are in when you log onto the ftp site. This is case sensitive
The file name is also case sensitive.
If you don't know the name, you can download all files with a csv extension.
If this remote folder will have all of the previous files, you can get a list of files then download only those you don't have, but this gets much more complicated.
If the file name includes the date, you can check for that as well.
If the filename starts with the date, you can also download all files that start with today's (or yesterday's) date.

Edit the first 6 *Set* statements with the correct info to get one file. Only change the part shown in red.



Code:


@Echo Off
Set _FTPServerName=ftp site address
Set _UserName=username
Set _Password=password
Set _LocalFolder=C:\FTPFiles
Set _RemoteFolder=Public\Reports\CSV
Set _Filename=report.csv
Set _ScriptFile=ftp1
:: Create script
 >"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo lcd %_LocalFolder%
>>"%_ScriptFile%" Echo cd %_RemoteFolder%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo get %_Filename%
>>"%_ScriptFile%" Echo quit
:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%"

Edit the first 5 *Set* statements in this version to get all *.csv files. Only change the part shown in red:


Code:


@Echo Off
Set _FTPServerName=ftp site address
Set _UserName=username
Set _Password=password
Set _LocalFolder=C:\FTPFiles
Set _RemoteFolder=Public\Reports\CSV
Set _Filename=*.csv
Set _ScriptFile=ftp1
:: Create script
 >"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo lcd %_LocalFolder%
>>"%_ScriptFile%" Echo cd %_RemoteFolder%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo prompt
>>"%_ScriptFile%" Echo mget %_Filename%
>>"%_ScriptFile%" Echo quit
:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%"

Ignore the spaces in front of the red text. The Forum editor insists on adding spaces. The red text starts immediatly after the =


----------



## kirubhagaran

I tried working out this codes by creating a batch file on my desktop with the name of "FTP.bat". I am unable to copy the files from the FTP to the local hard drive. the back command prompt window opens and stands still with a cursor blinking. 
Please help. how do we proceed with?

__________________________________________




TheOutcaste said:


> There are 6 things you need:
> 1 FTP Server Name (FTP Site name, either the name or IP address
> 2 UserName for the FTP Account
> 3 Password for the FTP Account
> 4 Local Folder, where on your PC you want to store the file
> 5 Remote Folder, where the file is on the FTP Server
> 6 Filename, the name of the file to retrieve
> 
> The Remote folder path should be relative to the folder you are in when you log onto the ftp site. This is case sensitive
> The file name is also case sensitive.
> If you don't know the name, you can download all files with a csv extension.
> If this remote folder will have all of the previous files, you can get a list of files then download only those you don't have, but this gets much more complicated.
> If the file name includes the date, you can check for that as well.
> If the filename starts with the date, you can also download all files that start with today's (or yesterday's) date.
> 
> Edit the first 6 *Set* statements with the correct info to get one file. Only change the part shown in red.
> 
> 
> 
> Code:
> 
> 
> @Echo Off
> Set _FTPServerName=ftp site address
> Set _UserName=username
> Set _Password=password
> Set _LocalFolder=C:\FTPFiles
> Set _RemoteFolder=Public\Reports\CSV
> Set _Filename=report.csv
> Set _ScriptFile=ftp1
> :: Create script
> >"%_ScriptFile%" Echo open %_FTPServerName%
> >>"%_ScriptFile%" Echo %_UserName%
> >>"%_ScriptFile%" Echo %_Password%
> >>"%_ScriptFile%" Echo lcd %_LocalFolder%
> >>"%_ScriptFile%" Echo cd %_RemoteFolder%
> >>"%_ScriptFile%" Echo binary
> >>"%_ScriptFile%" Echo get %_Filename%
> >>"%_ScriptFile%" Echo quit
> :: Run script
> ftp -s:"%_ScriptFile%"
> Del "%_ScriptFile%"
> 
> Edit the first 5 *Set* statements in this version to get all *.csv files. Only change the part shown in red:
> 
> 
> Code:
> 
> 
> @Echo Off
> Set _FTPServerName=ftp site address
> Set _UserName=username
> Set _Password=password
> Set _LocalFolder=C:\FTPFiles
> Set _RemoteFolder=Public\Reports\CSV
> Set _Filename=*.csv
> Set _ScriptFile=ftp1
> :: Create script
> >"%_ScriptFile%" Echo open %_FTPServerName%
> >>"%_ScriptFile%" Echo %_UserName%
> >>"%_ScriptFile%" Echo %_Password%
> >>"%_ScriptFile%" Echo lcd %_LocalFolder%
> >>"%_ScriptFile%" Echo cd %_RemoteFolder%
> >>"%_ScriptFile%" Echo binary
> >>"%_ScriptFile%" Echo prompt
> >>"%_ScriptFile%" Echo mget %_Filename%
> >>"%_ScriptFile%" Echo quit
> :: Run script
> ftp -s:"%_ScriptFile%"
> Del "%_ScriptFile%"
> 
> Ignore the spaces in front of the red text. The Forum editor insists on adding spaces. The red text starts immediatly after the =


----------



## TheOutcaste

You can't name it FTP. If you do, it will call itself instead of the FTP program, and do nothing but create and delete the ftp script file.


----------



## kirubhagaran

Hi. Thanks for your msg. Could you be kind enough to help me in this regard.
what should i name the batch file.

often when i click the batch file, it creates a file FTP1 on my desktop and does nothing. why is this happening? 

it would be much better if you could explain me the installation procedure to work on this script.




TheOutcaste said:


> You can't name it FTP. If you do, it will call itself instead of the FTP program, and do nothing but create and delete the ftp script file.


----------



## TheOutcaste

You can name it anything you want as long as it's not an existing command or program. Name it *GetMyFilebyFTP.Bat* if you want.
If you name it FTP.bat, when it tries to execute the FTP command, it looks first in the current folder for a program file named ftp, so it will find the batch file and run it again, instead of finding the *ftp.exe* file in *Windows\system32*

It's pretty self explanatory. Edit the 6 Set statements with your info. The color coding adds some goofy spacing though.

1 _FTPServerName: FTP Server Name (FTP Site name, either the name or IP address
2 _UserName: UserName for the FTP Account
3 _Password: Password for the FTP Account
4 _LocalFolder: The folder on your PC where you want to store the file
5 _RemoteFolder: The folder on the FTP Server that contains the file you want to download
6 _Filename: The name of the file to retrieve

You change only the part on the right of the equals sign.
Without the color coding those 6 lines will look something like this:


Code:


Set _FTPServerName=ftp://ftp.microsoft.com
Set _UserName=MyUserName    
Set _Password=MyPassword     
Set _LocalFolder=C:\FileGoesHere  
Set _RemoteFolder=Public\Reports
Set _Filename=report.csv


----------



## kirubhagaran

Wow! Thanks superman! it works.

now my next move is: 

1. it just copies files. what should i do to copy the folders and its complete subfolders?

2. how to auto detect and autodownload without manual intervention?

Will be able to assit me regarding this? hope i am not disturbing you.

Regards,
Kiru




TheOutcaste said:


> You can name it anything you want as long as it's not an existing command or program. Name it *GetMyFilebyFTP.Bat* if you want.
> If you name it FTP.bat, when it tries to execute the FTP command, it looks first in the current folder for a program file named ftp, so it will find the batch file and run it again, instead of finding the *ftp.exe* file in *Windows\system32*
> 
> It's pretty self explanatory. Edit the 6 Set statements with your info. The color coding adds some goofy spacing though.
> 
> 1 _FTPServerName: FTP Server Name (FTP Site name, either the name or IP address
> 2 _UserName: UserName for the FTP Account
> 3 _Password: Password for the FTP Account
> 4 _LocalFolder: The folder on your PC where you want to store the file
> 5 _RemoteFolder: The folder on the FTP Server that contains the file you want to download
> 6 _Filename: The name of the file to retrieve
> 
> You change only the part on the right of the equals sign.
> Without the color coding those 6 lines will look something like this:
> 
> 
> Code:
> 
> 
> Set _FTPServerName=ftp://ftp.microsoft.com
> Set _UserName=MyUserName
> Set _Password=MyPassword
> Set _LocalFolder=C:\FileGoesHere
> Set _RemoteFolder=Public\Reports
> Set _Filename=report.csv


----------



## TheOutcaste

If you need to copy an entire folder structure, you'd be better off using an FTP client for that. There are several free and paid versions to do that. Google for FTP Sync or FTP Retrieval. The NcFTP suite or Wget can also be used if you are wanting to download an entire directory structure every time.

Doing it from batch would require specifying each source and destination folder. If you don't know the folder names, you would have to create a script to log in, get a list of all files and folders and log out. Depending on the server, you may be able to get just a list of folders. If the server doesn't support doing a recursive listing, you would then have to create a script for each top level folder, then log in to get a list of the contents of each folder, log out, create a new script to check the next level, and repeat until each folder has been found.

Once you have a complete list of folders, you can build a script to actually download the files.

When you say autodetect it sounds like you just want to get new files. If you only want to download files that have been added since the last time, that gets even more complex. You would have to get a complete list of files, then compare them to what you have to downoad anything you don't already have, or check the file dates and compare it to the date of your last download. A 3rd party app would be much easier in this case.

Here's an example to get every file from three different folders:


Code:


@Echo Off
Set _FTPServerName=ftp site address
Set _UserName=username
Set _Password=password
Set _LocalFolder1=C:\FTPFiles\Folder1
Set _RemoteFolder1=/Public/Folder1
Set _LocalFolder2=C:\FTPFiles\Folder2
Set _RemoteFolder2=/Public/Folder2
Set _LocalFolder3=C:\FTPFiles\Folder1\Subfolder
Set _RemoteFolder3=/Public/Folder1/Subfolder
Set _Filename=*.*
Set _ScriptFile=ftp1
:: Create script
 >"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo prompt
>>"%_ScriptFile%" Echo lcd %_LocalFolder1%
>>"%_ScriptFile%" Echo cd %_RemoteFolder1%
>>"%_ScriptFile%" Echo mget %_Filename%
>>"%_ScriptFile%" Echo lcd %_LocalFolder2%
>>"%_ScriptFile%" Echo cd %_RemoteFolder2%
>>"%_ScriptFile%" Echo mget %_Filename%
>>"%_ScriptFile%" Echo lcd %_LocalFolder3%
>>"%_ScriptFile%" Echo cd %_RemoteFolder3%
>>"%_ScriptFile%" Echo mget %_Filename%
>>"%_ScriptFile%" Echo quit
:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%"


----------



## kirubhagaran

Thanks dude! you are very helpful.




TheOutcaste said:


> If you need to copy an entire folder structure, you'd be better off using an FTP client for that. There are several free and paid versions to do that. Google for FTP Sync or FTP Retrieval. The NcFTP suite or Wget can also be used if you are wanting to download an entire directory structure every time.
> 
> Doing it from batch would require specifying each source and destination folder. If you don't know the folder names, you would have to create a script to log in, get a list of all files and folders and log out. Depending on the server, you may be able to get just a list of folders. If the server doesn't support doing a recursive listing, you would then have to create a script for each top level folder, then log in to get a list of the contents of each folder, log out, create a new script to check the next level, and repeat until each folder has been found.
> 
> Once you have a complete list of folders, you can build a script to actually download the files.
> 
> When you say autodetect it sounds like you just want to get new files. If you only want to download files that have been added since the last time, that gets even more complex. You would have to get a complete list of files, then compare them to what you have to downoad anything you don't already have, or check the file dates and compare it to the date of your last download. A 3rd party app would be much easier in this case.
> 
> Here's an example to get every file from three different folders:
> 
> 
> Code:
> 
> 
> @Echo Off
> Set _FTPServerName=ftp site address
> Set _UserName=username
> Set _Password=password
> Set _LocalFolder1=C:\FTPFiles\Folder1
> Set _RemoteFolder1=/Public/Folder1
> Set _LocalFolder2=C:\FTPFiles\Folder2
> Set _RemoteFolder2=/Public/Folder2
> Set _LocalFolder3=C:\FTPFiles\Folder1\Subfolder
> Set _RemoteFolder3=/Public/Folder1/Subfolder
> Set _Filename=*.*
> Set _ScriptFile=ftp1
> :: Create script
> >"%_ScriptFile%" Echo open %_FTPServerName%
> >>"%_ScriptFile%" Echo %_UserName%
> >>"%_ScriptFile%" Echo %_Password%
> >>"%_ScriptFile%" Echo binary
> >>"%_ScriptFile%" Echo prompt
> >>"%_ScriptFile%" Echo lcd %_LocalFolder1%
> >>"%_ScriptFile%" Echo cd %_RemoteFolder1%
> >>"%_ScriptFile%" Echo mget %_Filename%
> >>"%_ScriptFile%" Echo lcd %_LocalFolder2%
> >>"%_ScriptFile%" Echo cd %_RemoteFolder2%
> >>"%_ScriptFile%" Echo mget %_Filename%
> >>"%_ScriptFile%" Echo lcd %_LocalFolder3%
> >>"%_ScriptFile%" Echo cd %_RemoteFolder3%
> >>"%_ScriptFile%" Echo mget %_Filename%
> >>"%_ScriptFile%" Echo quit
> :: Run script
> ftp -s:"%_ScriptFile%"
> Del "%_ScriptFile%"


----------

