How to write commands in a console program through a batch (.bat) file?

Asked

Viewed 2,597 times

9

I want to automate a routine through a .bat which will call a console program which, in turn, will receive successive commands from this .bat. This console program could be, for example, an ftp client.

Example:

c:> BaixarArquivo.bat MeuArquivo.ext

Contents BaixarArquivo.bat:

cd /d c:\DiretorioDescarga
ftp 111.222.333.444
ftp> mget %0       // Como fazer um 'output' destes
ftp> quit          //  comandos dentro da .bat?

Whereas . bat will wait for the end of the program call, how to pass these commands on to it?

3 answers

5

To pass parameters to a .bat:

echo off
echo %1

In the code above I passed the parameter number I want to pick, in case it will be only 1:

programa.bat argumento

You can also use the joker: %* that it takes all parameters that are passed separated by space.

To save the return of some command you can create a variable: set arg = retorno

  • I’m sorry for the misinterpretation of the question. For some reason someone changed the title of this topic, changing its original meaning.

3


You can direct inputs as follows:

(
echo mget %0
echo quit
) | ftp 111.222.333.444

However, please refer to your program’s documentation if it allows some execution parameters. It may accept a list of commands to be executed.

  • On the fly! That’s what I needed.

0

You don’t need this. You are using ftp.exe from Windows itself, right?

Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [host]

  -v              Suppresses display of remote server responses.
  -n              Suppresses auto-login upon initial connection.
  -i              Turns off interactive prompting during multiple file
                  transfers.
  -d              Enables debugging.
  -g              Disables filename globbing (see GLOB command).
  -s:filename     Specifies a text file containing FTP commands; the
                  commands will automatically run after FTP starts.
  -a              Use any local interface when binding data connection.
  -A              login as anonymous.
  -x:send sockbuf Overrides the default SO_SNDBUF size of 8192.
  -r:recv sockbuf Overrides the default SO_RCVBUF size of 8192.
  -b:async count  Overrides the default async count of 3
  -w:windowsize   Overrides the default transfer buffer size of 65535.
  host            Specifies the host name or IP address of the remote
                  host to connect to.

Notes:
  - mget and mput commands take y/n/q for yes/no/quit.
  - Use Control-C to abort commands.

Pay attention to the -s option that allows you to specify a file with the commands that should be executed once the program is executed.

Example of use:

ftp.exe -s:comandos.txt

Contents of the file commands.txt

open dominio.com.br
usuario
senha
quit

If that’s the case, all you have to do is edit the.txt commands so that it fits your needs with the name of the file you want to send or receive. Allowing you greater control over the operations you will perform on FTP.

  • In my case, yes. ftp is just an example to simplify my question. In my real case, I will make SQL queries through an Oracle client, return results, and more...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.