Typable Variable Prompt In A Batch File? - batch-file

Here is what my batch currently looks like:
srcds.exe +hostname "[FAST-DL]Ultra-Realistic Apocalypse Roleplay CustomRP ALPHA" +maxplayers 8 -console -authkey 8F1B5086400AC592380B5F303778D008 +gamemode darkrp +map gm_atomic
How would I make it so when the window opens, I can enter a custom variable for +map. | (optional) or I can continue using the variable I previously used last time the batch was run (in the case of the sample code, it would be gm_atomic) |

set "map="
set /p "map=Some prompt for map "
You can then detect whether someone entered a map using
if defined map (something) else (something_else)
or, perhaps
if defined map set "map=+map %map%"
then
srcde.exe ...whatever... %map%
which will add nothing if map was not entered, or +map whateverwasentered is there was an entry made.
You could supply a default if you wish:
set "map=whateveryourdefaultvalueis"
set /p "map=Some prompt for map [default:%map%]"
So your users need only press Enter to use the default you supply... (which may, with a little code, be derived from a file - the possibilities are limited by your imagination...)

Related

How to add an HP (health) variable to a batch game

So, I'm making a little batch CYOA style game, and I want to make an HP variable.
I used the code : set /p hp=100 which just shows up as 100 in the file when it's run.
I also can't use the code %hp% to make it visible when I want it to be!
Anybody got any simple ways to add a variable that can't be changed by the file's user, but can be changed if the code calls for it, and can be shown when the %hp% command is typed in?
Don't use /p. That's for getting an answer back from the user, given a prompt string. Example:
set /p age=How old are you?
Whatever the user enters will get set to age and can be used with %age%.
In your case, just do set hp=100, and then you can say echo HP: %hp% to display it. Done.
Also, you can type /? (as in set /?) in cmd to get help for a command.

Assign empty system variable using setx

I need a system variable and assign an empty string value to it. I've tried
setx samplepassword ""
But upon echo %samplepassword% , what should be an empty string is instead %samplepassword%. Now I found this in the docs
Setting value of "" (empty quotes) will appear to delete the variable
- it's not shown by SET but the variable name will remain in the registry.
http://ss64.com/nt/setx.html
How can I assign an empty system variable using setx?
Update:
I have checked the environment variables via the Windows GUI: Control Panel | System | Advanced | Environment Variables and saw that the DB_PASSWORD was indeed initialized with an empty string value. But why does executing echo %samplepassword% give me the above output? I have opened a new instance of cmd to be precise.
Technically you can create an empty environment variable, but not with setx.
Run that as administrator
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /v TESTENV /d ""
the registry key is added:
it even shows in environment variables system panel
But not in the list when invoking set
So even if it is possible, it is rather useless. However it could fool some programs testing if the env. variable exists by checking if registry key exists. So it could even be counter-productive and source of confusion.
I think you have a misconception here. There is no way to assign an
"empty string" to an environment (Batch) variable. The command set
"samplepassword=" deletes the variable, so echo "%samplepassword%"
just show "". You don't need setx command to do that, nor setx can
modify this behavior. – Aacini Aug 9 at 4:39
TL;DR: Technically it is not possible to create an empty variable in the environment. – Aacini
I had the same problem getting a file name with or without a suffix. You can work around this with a string prefix and string extraction (see here), e.g.
not empty string:
set COUNT=two
set SUFFIX=?s
echo %COUNT% thing%SUFFIX:~1%
gives two things
empty string
set COUNT=one
set SUFFIX=?
echo %COUNT% thing%SUFFIX:~1%
gives one thing
With the %SUFFIX:~1% you dismiss the string prefix (in this example ?) and get the rest of the string. This rest can be empty.

Batch file run through windows Task Scheduler

I have created a Task Scheduler in windows to run a batch file. This batch file in turn is calling a cmd file. The cmd file is expecting some user input. Could you please let me know how I can provide input to this automatic process?
The message being shown is as follows:
[input] Where is your product view ?
... how I can provide input to this automatic process?
Suppose myCmd.cmd contains next line:
set /p "input=[input] Where is your product view ?"
Create file myInput.txt with appropriate answer as follows:
here is my product view
and in your batch file use < redirection as follows:
call myCmd.cmd<myInput.txt
I guess you're doing it like this:
ECHO Where is your product view ?
SET /p input=
So then you could simply set the input variable from a parameter instead:
SET input=%*
This would get the parameters when calling the BATCH-file. file.bat "Here is my product view."

how to clear a variable which is set from command line by "set /p var1="

In Windows batch files, I accept variable from user input by set /p var1=, after var1 is used, I don't know how to reset/clear its value.
If I don't reset/clear its value, when user meets set /p var1= again, and user enter directly. the previous input value will be still there. I don't want it, How to reset it for new user input?
To clear a variable, regardless how it was set:
set "var1="
rare issue, but you can try this:
set "var=%var*=%"
Different answers to original question were already provided above. One additional hint: within batch-script you can use the scopes:
setlocal
...
endlocal
This ensures that the variables are only accessible within your batch script and not affecting variables outside of your batch script (i.e. your variables are also not accessible outside of the batch script). By exiting of the batch script - the variables used locally get automatically "cleared".

DOS batch files: How to write to the prompt and stay in the same line to input text

I like to know if there is a way to read the user input inside a batch file, because i have a file named: "fif.bat" that recives two parameters (just call them paramA and paramB) so i execute the file like this:
fif paramA paramB
I have to change paramA every month, but i call this file lot of times so i like to open a console and have printed this:
fif paramA
So i only have to write paramB and change paramA when i want it.
PD: paramA is very large so it's very helpful if i can have it there instead of writing every time. And i don't want to make another batch file to call fif whit paramA.
I think this might be what you're looking for:
#ECHO OFF
SET /p paramA=Parameter A:
ECHO you typed %paramA%
PAUSE
Line one stops commands in batch file from being echoed to the console
Line two prompts the user with "Parameter A:" and waits for user to enter a value and press enter. The value goes into a variable called paramA.
Line three echoes the value of the variable paramA to the console
Line four waits for the user to hit any key.
Note that the SET /p command does not work on every version of windows, I beleive it was introduced in 2000, but I could be wrong on the version.
You can prompt for user input in a batch file using SET /P for example:
SET /P paramB="Prompt String: "

Resources