How to declare a variable in . BAT ("shell")?

Asked

Viewed 15,108 times

2

I’m starting to mess with file. BAT, and I created a command line. Example:

ping <nome do computador>

Below it would show me if it is online, would look like this:

ping computador_1

Imagem do arquivo .BAT no Bloco de Notas e resultado no prompt de comando

But I wanted to put the name of the PC or IP in a variable, and use this variable in the command ping. How can I do that?


Pedro Gaspar

The mistake is this ! below is showing my commands and what appears the file that is as "white" name in the folder it Abaré and appears and closes (as per the image attached) and yes I have a folder named ping.bat and is described as in the imageinserir a descrição da imagem aqui


Gentlemen, my command line has gone like this only that @echo off set /a nome=Enter the destination name or IP: echo The chosen name was '%name%'. echo %errorlevel% ping.exe %name%

and the ping.exe I call it (yes it is in the same folder) was the following

set/p host=Type the machine name ping %machine%

NOTE: I WANT TO BE able to put Ip or Machine Name on the type command line on the first command line it opens and only need to put what I want.

On this command line (NO NEED TO CALL ANOTHER . bat)

  • 3

    Welcome to Stackoverflow Caique. Avoid placing images with the code, always prefer to write the code directly in the question.

  • What you want is to pass a parameter to the script, not declare a variable, right? Use %1 for the first command line argument, %2 for the second and so on until %9.

1 answer

1

The way you did it is already correct to use the variable in a batch script. The command set is used to assign a value to a variable and the command set /p allows Voce to specify a text that will be presented to the user and the variable value will be a user input.

You could also use the command set /a to specify a numerical expression, as in set /a soma=4+3.

And then you use the variable along the script by placing its name between percentage signals: %<nome da variáve>%.

But you can use the command echo to control what will be displayed as a result of your batch script. The command @echo off does not display each executed command, and when you use the echo directly with some text, this text is displayed at the command prompt.

See your slightly modified batch script test:

[Test bat.]

@echo off
set /p nome=Informe o nome ou IP do destino: 
echo O nome escolhido foi '%nome%'.
ping.exe %nome%

The result would be:

c:\>teste
Informe o nome ou IP do destino: pt.stackoverflow.com
O nome escolhido foi 'pt.stackoverflow.com'.

Pinging pt.stackoverflow.com [151.101.65.69] with 32 bytes of data:
Reply from 151.101.65.69: bytes=32 time=5ms TTL=61
Reply from 151.101.65.69: bytes=32 time=6ms TTL=61
Reply from 151.101.65.69: bytes=32 time=5ms TTL=61
Reply from 151.101.65.69: bytes=32 time=6ms TTL=61

Ping statistics for 151.101.65.69:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 5ms, Maximum = 6ms, Average = 5ms

Editing

The colleague bfavaretto warned that perhaps the doubt was in relation to passing parameters to the file. BAT, and that the parameters can be recovered in the script using the variables %1, %2, etc., until %9. The number in the variable will refer to the order of the parameter in the command line.

For example, the following batch script:

@echo off
echo %1
echo %2

It could be called that, and that would be the result:

C:\teste.bat parâmetro1 /parâmetro2
parâmetro1
/parâmetro2

Issue 2

Making the first batch with the concept of parameters would look like this:

[Test bat.]

@echo off
echo O nome ou IP do destino escolhido foi '%1'.
ping.exe %1

And it could be called that, and that would be the result:

C:\teste.bat pt.stackoverflow.com
O nome ou IP do destino escolhido foi 'pt.stackoverflow.com'.

Pinging pt.stackoverflow.com [151.101.193.69] with 32 bytes of data:
Reply from 151.101.193.69: bytes=32 time=6ms TTL=59
[...]
  • but if it is correct why it does not show the result (does not copy)? I copied this your test. @echo off set /p name=Enter destination name or IP: echo The chosen name was '%name%'. ping %name%

  • Strange, here for me the result was exactly the one I posted. And if you fire the command ping at command prompt directly, no batch script, works?

  • yes works without nor a problem works both for both site for ip and even serves for pc name.

  • Puts a echo %errorlevel% in the line following the line of ping %nome% in the file . BAT, to see if there is an error in the command ping, when fired by batch. You have some file called ping.bat in that folder? Try placing the command ping.exe %nome% in the archive . BAT.

  • and I wanted to specify the location when I opened the same .. not in another file.

  • Pedro Gaspar, your answer is good, but I think the question in the background is how to pass parameters to the script on the command line.

  • That might be it, right. I’ll add that to the answer then, thank you.

Show 2 more comments

Browser other questions tagged

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