Question Locked

Script request for FTP

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I would like to write a script that would enable me to transfer a *.dat file from my machine to a server with an ip address.
Currently, I do this manually, and below are the commands I type in my DOS prompt window:

C:\>ftp x.x.x.
username
password
ascii
cd ai_pos_rcv
cd ai_root:[pos_rcv]
ls
put dbtr0xxx.dat
quit

How do I employ my inputs into a script so that I can run the script with all my input commands?

Appreciate it.

4 replies

by slayer_

I had something similar for an automated backup.

Basically, the FTP program can read a series of commands from an input file. You can make a basic text file with the connect commands, password, etc. and just pass this file path as a parameter along with a switch.

by churdoo

As Sinister states, and as you suspect, this is possible. The -s option of the FTP command allows you to pass a file with a series of commands to the FTP program. The trick however, is that the ftp command file is static and does not have any variable processing in it like a batch file does.

So you'll have to write a batch file which takes the variable(s) and generates the FTP command file at run time based on the supplied value(s), and then calls FTP with the -s option and the generated command file.

Which are the variables? Only the file name on the PUT line, or is the FTP server IP a variable as well?

Your batch ftpfileput.bat can look something like:
Rem Usage: ftpfileput filename
Rem where filename = existing local file to FTP PUT to server x.x.x.x
Rem %1 = supplied filename to FTP PUT
Rem
if not exist %1 goto noFile
Rem
Rem the first echo below uses < redirect to create the command file
Rem subsequent echo uses << redirect to append to the command file
Rem therefore you're building the file line-by-line
Rem
echo open x.x.x.x >ftpcommand.ftp
echo username >>ftpcommand.ftp
echo password >>ftpcommand.ftp
echo ascii >>ftpcommand.ftp
echo cd ai_pos_rcv >>ftpcommand.ftp
echo cd ai_root:[pos_rcv] >>ftpcommand.ftp
echo put %1 >>ftpcommand.ftp
echo bye >>ftpcommand.ftp
Rem ready to run the FTP
ftp -s:ftpcommand.ftp
goto done
:noFile
echo .
echo %1 does not exist!
:done
echo .
echo .
echo Done!

This is rough, but you'll get the idea. I can think of another method using the copy command and the + operator to concatenate different file stubs into a complete command file, but for a command file this short it's just as easy to use the echo and output redirects to build the command file line by line.

by wats679

Thank you SinisterSlay and Churdoo. I am a total newbie into scripting so i wouldn't really have much of a clue, but I will definitely try out that solution pasted above by Churdoo!

Churdoo, the only variable would be the put line as the filename would change on each transfer. It's a sequenced filename.

My understanding thus far is:
1. One file would contain my inputs:
ftp a.b.c.d
username
password
ascii
cd ai_pos_rcv
cd ai_root:[pos_rcv]
ls
put filename.dat
bye

2. The other write-up would be as you've provided above.

3. now I'll just have to figure out a way in incorprate my inputs into your scripts, am I right?

Thank you for your help so far! Much is still needed and appreciated:)

This discussion is closed to new replies.