# Batch Script to FTP



## Codesearcher (May 13, 2009)

Hi All,
I need a batch script to ftpthe files in a folder (Windows Server) to Unix environment.
Is it possible to ftp *without creating the filelist*.
Do we have command to transfer all the files in the folder to UNIX without creating a filelist?
Any other scripting language is also welcome.. but the requirement is to transfer the files without creating filelist.

Thanks in advance.


----------



## deleted122510 (Mar 18, 2009)

I don't get it, why not just upload it, what's gonna happen?


----------



## TheOutcaste (Mar 19, 2009)

Welcome to TSF!

This will upload all files in the specified local folder (_lcd variable) to the specified folder on the FTP server (_FTPRoot variable). It doesn't create a file list, just uploads everything.
You do have to create the script file. Note that the username and password are stored in the file, so anyone with read access to the folder that contains the batch file can see the username and password. You can always prompt for the UN/PW when the batch file is run if needed.
Edit the *server name, username*, and *password* as needed
Specify the *Local folder* and the *FTP Server folder* as needed


```
@echo off
SetLocal
:: Set local folder you want to upload here.
Set _lcd=[B]C:\Folder to upload[/B]
:: Will use current folder as a Temp folder for the FTP Script
Set _tmpfld=%~dp0
Set _tmpfld=%_tmpfld:~0,-1%
:: Set the Folder on the FTP server that you want to upload to
Set _FTPRoot=[B]/[/B]
 >"%_tmpfld%\ftpcmds.}t{" Echo open [B]ftp.server.com[/B]
>>"%_tmpfld%\ftpcmds.}t{" Echo [B]Username[/B]
>>"%_tmpfld%\ftpcmds.}t{" Echo [B]password[/B]
>>"%_tmpfld%\ftpcmds.}t{" Echo cd "%_FTPRoot%"
>>"%_tmpfld%\ftpcmds.}t{" Echo lcd "%_lcd%"
>>"%_tmpfld%\ftpcmds.}t{" Echo binary
>>"%_tmpfld%\ftpcmds.}t{" Echo mput *.*
>>"%_tmpfld%\ftpcmds.}t{" Echo quit
ftp -v -i -s:"%_tmpfld%\ftpcmds.}t{"
:: Delete the script
Del "%_tmpfld%\ftpcmds.}t{"
EndLocal
```
HTH

Jerry


----------



## Codesearcher (May 13, 2009)

Hi Jerry,
I am using the following script but it does not ftp the xml files.

@echo off
SetLocal
:: Set local folder you want to upload here.
Set _lcd=\\lxtrs416.mgroupnet.com\Archives\script\test06\DSR
:: Will use current folder as a Temp folder for the FTP Script
Set _tmpfld=%~dp0
Set _tmpfld=%_tmpfld:~0,-1%
:: Set the Folder on the FTP server that you want to upload to
Set _FTPRoot= /bcmf/SSA/BLUECUBE/intin/DSR
>"%_tmpfld%\ftpcmds.}t{" Echo open 89.108.1.19
>>"%_tmpfld%\ftpcmds.}t{" Echo user
>>"%_tmpfld%\ftpcmds.}t{" Echo user
>>"%_tmpfld%\ftpcmds.}t{" Echo cd "%_FTPRoot%"
>>"%_tmpfld%\ftpcmds.}t{" Echo lcd "%_lcd%"
>>"%_tmpfld%\ftpcmds.}t{" Echo binary
>>"%_tmpfld%\ftpcmds.}t{" Echo mput *.*
>>"%_tmpfld%\ftpcmds.}t{" Echo quit
ftp -v -i -s:"%_tmpfld%\ftpcmds.}t{"
:: Delete the script
Del "%_tmpfld%\ftpcmds.}t{"
EndLocal

Kindly correct if i were wrong anywhere..


----------



## Codesearcher (May 13, 2009)

Jerry sorry for the previous reply its working fine now.. Thanks a lot..
There was a space in the line Set _FTPRoot= /bcmf/SSA/BLUECUBE/intin/DSR


----------



## TheOutcaste (Mar 19, 2009)

You're welcome.:smile:

Glad you spotted that; an extra space can be a pain to find


----------



## Codesearcher (May 13, 2009)

Hi i have an enhance to the existing code.

While the FTP completes the same sorce xmls need to be archived to C:\Myfiles\Archive.
This is a transfer between Windows server only.

To summarize

1. The files from source should be Uploaded to UNIX server
2. Archived in the folder (under Windows Server)
3. deleted from Source directory.
Kindly advice me.

Regards,
Coddy.


----------



## TheOutcaste (Mar 19, 2009)

Would probably be better to copy them after uploading rather than trying to do it at the same time. You could start a 2nd batch file to do the archiving just before the FTP starts, but depending on the setup, you might not be able to copy a file that is currently being uploaded.
I'd expect the LAN to be faster than an FTP Upload, but that might not be the case, so you'd need which ever script is NOT going to do the deleting to create a flag file when it is done, and the other script would have to check for that file before deleting.
Easiest to just add the two red lines shown below. type *xcopy /?* for a list of switches to see if you need to add any others. You could also use Move instead of Xcopy, then you wouldn't need the Delete.

```
ftp -v -i -s:"%_tmpfld%\ftpcmds.}t{"
:: Delete the script
Del "%_tmpfld%\ftpcmds.}t{"
Xcopy /CIQ "%_lcd%\*.xml" C:\Myfiles\Archive
Del /F /Q "%_lcd%\*.xml"
EndLocal
```
HTH

Jerry


----------



## Codesearcher (May 13, 2009)

hey when i try upload files with this method i have got a problem...
this script is working great with few number of files... but when it comes to files like 500 and more only 100-125 files are getting FTPed.....
Could please help me on this????
Could there be any other reason for this problem......
Kindly help me..


----------



## TheOutcaste (Mar 19, 2009)

Are you getting any error messages? Or does the FTP session just quit?
Any chance the FTP Server has a time limit on connections?


----------



## Codesearcher (May 13, 2009)

It just transfered few xml files and the FTP session quits....
There is no time limit on the connections too....


----------



## TomE1 (Jun 1, 2009)

The ftp script works great as long as the _tmpfld is a path thas does not contain any spaces in the path name. How can you add quote marks to the path. Vista can be set where you can not write C:\. so you need to write the _tmpfld to the desktop or my documents.


----------



## TheOutcaste (Mar 19, 2009)

*_tmpfldr* can contain spaces. It _is_ quoted everyplace it needs to be: *"*%_tmpfld%\ftpcmds.}t{*"* .
If you can't write to C:\, you can't put the batch file there in the first place. Since it sets _tmpfldr to the same folder that contains the batch file, not being able to write to C:\ won't be a problem. Unless you change security settings after putting the file there of course.

Afraid I don't have any new ideas on why the connection would drop though.


----------

