coolkid
coolkid

Reputation: 543

input parameter for command need user manual input in batch programming

I'm using windows batch programming for creting a ftp connection to a ftp host an automatically upload file to that host. However, ftp command does not have parameter to get username and password in the command and I must input it at the subsequent command. How could the batch file wait for the ftp connection establish then echo the username and password for the subsequent ftp command? Thanks for helping.

Upvotes: 2

Views: 3414

Answers (2)

coffeetocode
coffeetocode

Reputation: 1233

There's actually a different way to do this.

Windows ftp accepts a "-s:filename" argument containing commands to be run. You could store the username/password there in the format "USER username\nPASSWD" pass, and they'll be run first thing when the connection is established.

Make sure to delete the file afterward, or better yet use a temp file that doesn't hit the disk ;)

EDIT: Clarifying for coolkid. This really is your answer (thanks for the backup Joey).

To be explicit, here you are.

Contents of upload.cmd:

@echo off 
set /p name= Username?
set /p pass= Password?

REM Give dummy data to the human-friendly FTP prompts
REM We will pass actual FTP commands in the next stanza
echo dummy > ftp_commands.txt
echo dummy >> ftp_commands.txt

echo USER %name% >> ftp_commands.txt
echo PASS %pass% >> ftp_commands.txt
echo PUT ftp_commands.txt >> ftp_commands.txt

ftp -s:ftp_commands.txt ftp.mydomain.com

Running upload.cmd:

C:\>upload.cmd
 Username?anonymous
 Password?foobar
Connected to ftp.mydomain.com.
220 2k-32-71-e Microsoft FTP Service (Version 5.0).
User (ftp.mydomain.com:(none)):
331 Password required for dummy .

530 User dummy  cannot log in.
Login failed.
ftp> USER anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.

230 Anonymous user logged in.
ftp> PUT ftp_commands.txt
200 PORT command successful.

Upvotes: 1

Jon
Jon

Reputation: 437474

You can echo some data to a temporary file and then redirect that file into the standard input of the ftp command. For example:

>file  echo USERNAME
>>file echo.
>>file echo PASSWORD
>>file echo.
ftp < file
rem Now we can delete the file
del file

If you did not need to have newlines (necessary to simulate the ENTER keypress when the ftp program asks for credentials) then you could have managed without a temporary file:

echo USERNAME | ftp

Upvotes: 1

Related Questions