Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 587 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In Windows cmd, how do I prompt for user input and use the result in another command?

#1
I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands.

For example, I'd like to accept a process ID from the user, and then run jstack against that ID, putting the results of the jstack call into a file. However, when I try this, it doesn't work.

Here's my sample bat file contents:

@echo off
set /p id=Enter ID:
echo %id%
jstack > jstack.txt

and here's what shows up in jstack.txt:

<pre>
Enter ID: Terminate batch job (Y/N)?
</pre>
Reply

#2
Dollar signs around the variable do not work on my Vista machine, but percent signs do.
Also note that a trailing space on the "set" line will show up between the prompt and user input.
Reply

#3
I am not sure if this is the case for all versions of Windows, however on the XP machine I have, I need to use the following:

set /p Var1="Prompt String"

Without the prompt string in **quotes**, I get various results depending on the text.
Reply

#4
@echo off
set /p input="Write something, it will be used in the command "echo""
echo %input%
pause

if i get what you want, this works fine. you can use %input% in other commands too.

@echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause
Reply

#5

@echo off
:start
set /p var1="Enter first number: "
pause


Reply

#6
Just added the

set /p NetworkLocation= Enter name for network?

echo %NetworkLocation% >> netlist.txt

sequence to my netsh batch job. It now shows me the location I respond as the point for that sample. I continuously >> the output file so I know now "home", "work", "Starbucks", etc. Looking for clear air, I can eavulate the lowest use channels and whether there are 5 or just all 2.4 MHz WLANs around.
Reply

#7
There is no documented /prompt parameter for SETX as there is for SET.

If you need to prompt for an environment variable that will survive reboots, you can use SETX to store it.

A variable created by SETX won't be usable until you restart the command prompt. Neatly, however, you can SETX a variable that has already been SET, even if it has the same name.

This works for me in Windows 8.1 Pro:

set /p UserInfo= "What is your name? "
setx UserInfo "%UserInfo%"

(The quotation marks around the existing variable are necessary.)

This procedure allows you to use the temporary SET-created variable during the current session and will allow you to reuse the SETX-created variable upon reboot of the computer or restart of the CMD prompt.

(Edited to format code paragraphs properly.)

Reply

#8
Just to keep a default value of the variable. Press Enter to use default from the recent run of your .bat:

@echo off
set /p Var1=<Var1.txt
set /p Var1="Enter new value ("%Var1%") "
echo %Var1%> Var1.txt

rem YourApp %Var1%

In the first run just ignore the message about lack of file with the initial value of the variable (or do create the Var1.txt manually).
Reply

#9
The syntax is as such: `set /p variable=[string]`

Check out

[To see links please register here]

or

[To see links please register here]

for a more deep dive into user input with the different versions of Windows OS batch files.

Once you have set your variable, you can then go about using it in the following fashion.

@echo off
set /p UserInputPath=What Directory would you like?
cd C:\%UserInputPath%

note the `%VariableName%` syntax



Reply

#10
I have a little cmd I use when preparing pc to clients: it calls the user for input, and the rename the pc to that.

@ECHO "remember to run this as admin."
@ECHO OFF
SET /P _inputname= Please enter an computername:
@ECHO Du intastede "%_inputname%"
@ECHO "The pc will restart after this"
pause
@ECHO OFF
wmic computersystem where name="%COMPUTERNAME%" call rename name="%_inputname%"

shutdown -r -f
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through