I'm trying to make a batch file with choices. I use:
ERRORLEVEL
or
set /P c=Choose..
if /I "%c%" EQU "1" goto :place1
What I can't figure out is - how to keep choice input for later. If I input 32, then I want to store it for later use, like:
start batchfile32
Batchfile is always the same, start Batchfile&%choice% is enough. The syntax for this eludes me however.
What have you tried? Because this seems to works:
set /P c=Choose..
start Batchfile%c%
Gives output:
Choose..32
The system cannot find the file Batchfile32.
Edit: Of course I do not have a file named Batchfile32, but the system is trying to start it. Showing that the variable is used correctly.
check.bat
set /p ck=
start sample%ck%
sample5.bat
calc
output
5
a calc will be open if user input is 5
Related
Key Generator
#ECHO OFF
COLOR A
ECHO Generating Key!
choice /d y /t 3 > nul
set /p "genkey"="%random%-%random%-%random%-%random%"
PAUSE
EXIT
Batch 2
COLOR A
#ECHO OFF
set /p base=
if %base% == %genkey% GOTO :ecs
:ecs
PAUSE
EXIT
The way I normally do this is by writing to a file and using SET to recall from the file.
For example:
BATCH FILE 1
echo off
set var1=%Random%-%Random%-%Random%
echo %var1%>temp.log
pause
exit
BATCH FILE 2
echo off
set Var1=nul
if EXIST Temp.log (set /p Var1=<Temp.log && del /Q Temp.log)
echo %Var1%
pause
exit
In this case, if you run the second batch file without running the first one, the output will be "nul". However, if you ran the first batch file before the seccond, the output of the first will be displayed.
You can change %Random%-%Random%-%Random% to whatever text or variable you want.
The program acts like the type function, however with this method it prints the contents of the file to a variable.
One last thing to note is that this method will ONLY read the first line of the file. This is useful where you are transfering numbers, then using that number in an operation. If you want to transfer the whole file, you can use a FOR state ment, but also note, the FOR statement will recall the entire into a singe line.
I have been writing some batch files now-a-days. I am beginner !. So i have made a custom batch in which by entering a setup name it launches it but. I'am having some problem creating this custom file.
#echo off
set /p lnk="Setup Name = "
if "%lnk%"=="install.itunes.x64.windows" goto itunes
:itunes
start=(path)(setup.exe).....
cls
But if a user enters "itues or "installitunes" or "KJEWBFciou" whatever that don't matchs my custom command I want a error Pop-up in this condition.
What can i Do?
and don't ask to put "if not "%lnk%" i have already tried help level:0
Because i have many setups like itunes if input will not equal to custom command it launches the next setup.
Please help me
Please igonre my errors i only made 'em here not in batch file.
in line 2 %lnk% , lnk
and line 3 "%lnk" ,"%lnk%"
Ok so..
1 #echo off
2 set /p %lnk%="Setup Name = "
3 if "%lnk%=="install.itunes.x64.windows" goto itunes
4 :itunes
5 start=(path)(setup.exe).....
6 cls
A few errors but you're close.
In line 2, you use:
set /p %lnk%=="Setup Name = " goto itunes
When setting a variable you can't use %% around it, but thats only used when comparing, because when creating the variable, the computer will replace [set /p %lnk%=] with [set /p =] which is invalid syntax.
In line 3:
if "%lnk%=="install.itunes.x64.windows" goto itunes
You never closed the quotes on the left of the '==' comparison. Do note you can also use [if %val1% equ %val2%] to the same results, which can help when you want to use other comparison tags.
A sidenote for the task you have set, although [goto itunes] works fine, its a good habit to use [goto :itunes] instead, and if you want to keep your code all together, you can just make a code block like:
if %val1% equ %val2% (
rem do stuff here
)
do note, if you either want a task to run if variables match, and if not try the next match, you can use multiple of these. Otherwise you can use:
if %val1% equ %val2% (
rem do stuff here
) else (
rem do other stuff here
)
In response to your issue on it launching the next command, thats because in line 3, you check if the variable matches your string, but if it doesnt batch skips it and runs the next line, which is your :itunes label.
All in all, this should work better, after you fix [start=(path)(setup.exe).....] to launch as desired.
#echo off && color f0 && title Itunes
:top
cls
set /p lnk="Setup Name = "
if "%lnk%"=="install.itunes.x64.windows" (
start=(path)(setup.exe).....
cls
)
cls
echo "%lnk%" was not matched to any choices...
pause
goto :top
:: _Arescet
I'm having trouble with a batch file. I need it to remember somthing for a long
period of time. Lets say I have a variable called %Rememberme%: I need to remember this for lets say a year for some reason. How can I make my batch file remember that variable?
Well... I could echo the variable to a file using the command
echo >>%Rememberme% C:\File.txt
Well the thing is I can't have that. I need it to be remembered some other way.
Or somehow I need to give the batch file administrator rights so that it can read or write to a file. Is there a way to do this?
You can use environment variables to do this, but be careful not to overwrite existing variables.
EX:
SETX REMEMBERME "C:\windows\system32"
And then in another file,
>echo %REMEMBERME%
>C:\windows\system32
The documentation for SETX is here: TechNet - SETX:
Important remark from the link:
Setx provides the only command-line or programmatic way to directly and permanently set system environment values. System environment variables are manually configurable through Control Panel or through a registry editor. The set command, which is internal to the command interpreter (Cmd.exe), sets user environment variables for the current console window only.
Here's an example of saving a variable in a file name. This obviously assumes the variable is limited to filename-friendly characters (like a number).
:: Reading variable
for %%f in (*.myvariable) do (
set myvariable=%%f
)
:: Remove extension
set myvariable=%myvariable:~0,-11%
:: Setting variable
del *.myvariable
echo. > "%myvariable%.myvariable"
echo %myvariable%
You can also store the variable inside the text file, but this requires opening the file in order to read the variable.
:: Reading variable
for /F "tokens=*" %%A in (myvariable.txt) do (
set "myvariable=%%A"
)
:: Setting variable
echo %myvariable%> myvariable.txt
echo %myvariable%
Your question is not clear. It have not any example nor a clear description of the request, so I can only guess...
The obvious point first: how to have a variable in a Batch file that remember its value for a long period of time? (i.e.: always) Easy: just define the variable in the Batch file:
set Rememberme=The value to remember
Of course, previous method does not allows to change the value of the variable, but you had mentioned nothing about this point in your request: "the value of the variable may be changed"...
Although SETX command should work, it requires admin rights and SETX is not designed for cases like this one. For example, what happens if the Batch file is changed or is not used anymore in a given computer? Well, the last value of the variable will remain in such computer for ever!
The second obvious solution is to write the variable into a separate file and read it from there when the Batch file start. If the separate file is a data file, the method described by Matt Johnson should work and it does not require administrator rights. You must note that the separate file may also be a Batch file, so the value of the variable may be read via a simple call statement:
rem Save the value
echo set Rememberme=%Rememberme%> setTheValue.bat
rem Read the value
call setTheValue.bat
However "the thing is you can't have that, so you need to be remembered some other way", although you don't explain the reasons for this restriction...
Another solution (that I think is what you are looking for) is to set the definition of the variable in the same Batch file:
rem Read the variable at beginning of the Batch file:
call :setTheValue
echo Value = %Rememberme%
rem The rest of the Batch file goes here
. . .
rem If the value needs to be changed:
if %changeValue% equ "yes" (
rem Do it:
echo set Rememberme=%Rememberme%>> "%~F0"
)
rem End the Batch file
goto :EOF
rem Define the subroutine that set the value:
:setTheValue
set Rememberme=Initial value of the variable
In previous code "%~F0" represent the name of the running Batch file itself. This way, each change in the variable value will append a new line at the bottom of the Batch file.
first:
echo >>%Rememberme% C:\File.txt
is wrong. It tries to write the string "C:\File.txt" into a filename referenced by the variable %rememberme%. What you want is:
echo %Rememberme%>>c:\File.txt
(or only one > to overwrite the file)
Second: if you have no permission to write to the root-directory, use another directory, where you have, for example:
echo %Rememberme%>%userprofile%\file.txt
or
echo %Rememberme%>%public%\file.txt
Note: be sure, there is no space between %Rememberme% and >; it would become part of the string, if you try to read it back.
to read it back, use:
set /p "remembered=" <%public%\file.txt
Here. This should help.
#echo off
echo What would you like to do?
echo 1. Enter your name
echo 2. Load your Name
choice /c 12
if %errorlevel% equ 1 goto newname
if %errorlevel% equ 2 goto loadname
:newname
cls
echo Input your name
set /p name=Name:
echo Your Name is saved!
::Here is the command where your name saves to a file.
(
echo %name
)>yourname.name
pause
exit /b
:loadname
cls
::Here is the command where it loads your name
(
echo %name%
)<yourname.name
echo Your Name: %name%
pause
exit /b
I've made a Program that Launches other programs, but here is the problem.
You need to specify the path of the file in the code which means that the end-user needs to get into the code to specify the file which is not really the ideal situation.
I have solution in mind,when you launch the program a dialog box comes up and asks you to give it the file path so it can run the specified program. How would I go about doing something like this?
You can also do the following
#echo off
set foo=%1
echo %foo%
%1 refers to the 1st parameter that you have passed it to the program. This first parameter will then be set to the variable %foo%. Here is the example:
C:\>test.bat "C:\passwd"
C:\passwd
Hope this helps :)
Update
You can make your program to execute another program by doing the following:
#echo off
REM `%~f1` will helps to expand `%1` to a fully qualitified path name
set "executable=%~f1"
REM checks if the first parameter exist. If it did not exist, a usage text will be displayed and the program will exit
if "%executable%"=="" (
echo Usage: %0 path\to\executable
goto :EOF
) else (
call :program
goto :EOF
)
:program
echo %executable% is starting...
start "" "%executable%"
goto :EOF
You can reads user input. e.g. edit the following snippet according to you need
ECHO User will have to enter the input file path.
set /p variable=Enter input files path please:
The user can type as many letters as they want, and it will go into the delBuild variable.
I have a batch file and I want to include an external file containing some variables (say configuration variables). Is it possible?
Note: I'm assuming Windows batch files as most people seem to be unaware that there are significant differences and just blindly call everything with grey text on black background DOS. Nevertheless, the first variant should work in DOS as well.
Executable configuration
The easiest way to do this is to just put the variables in a batch file themselves, each with its own set statement:
set var1=value1
set var2=value2
...
and in your main batch:
call config.cmd
Of course, that also enables variables to be created conditionally or depending on aspects of the system, so it's pretty versatile. However, arbitrary code can run there and if there is a syntax error, then your main batch will exit too. In the UNIX world this seems to be fairly common, especially for shells. And if you think about it, autoexec.bat is nothing else.
Key/value pairs
Another way would be some kind of var=value pairs in the configuration file:
var1=value1
var2=value2
...
You can then use the following snippet to load them:
for /f "delims=" %%x in (config.txt) do (set "%%x")
This utilizes a similar trick as before, namely just using set on each line. The quotes are there to escape things like <, >, &, |. However, they will themselves break when quotes are used in the input. Also you always need to be careful when further processing data in variables stored with such characters.
Generally, automatically escaping arbitrary input to cause no headaches or problems in batch files seems pretty impossible to me. At least I didn't find a way to do so yet. Of course, with the first solution you're pushing that responsibility to the one writing the config file.
If the external configuration file is also valid batch file, you can just use:
call externalconfig.bat
inside your script. Try creating following a.bat:
#echo off
call b.bat
echo %MYVAR%
and b.bat:
set MYVAR=test
Running a.bat should generate output:
test
Batch uses the less than and greater than brackets as input and output pipes.
>file.ext
Using only one output bracket like above will overwrite all the information in that file.
>>file.ext
Using the double right bracket will add the next line to the file.
(
echo
echo
)<file.ext
This will execute the parameters based on the lines of the file. In this case, we are using two lines that will be typed using "echo". The left bracket touching the right parenthesis bracket means that the information from that file will be piped into those lines.
I have compiled an example-only read/write file. Below is the file broken down into sections to explain what each part does.
#echo off
echo TEST R/W
set SRU=0
SRU can be anything in this example. We're actually setting it to prevent a crash if you press Enter too fast.
set /p SRU=Skip Save? (y):
if %SRU%==y goto read
set input=1
set input2=2
set /p input=INPUT:
set /p input2=INPUT2:
Now, we need to write the variables to a file.
(echo %input%)> settings.cdb
(echo %input2%)>> settings.cdb
pause
I use .cdb as a short form for "Command Database". You can use any extension.
The next section is to test the code from scratch. We don't want to use the set variables that were run at the beginning of the file, we actually want them to load FROM the settings.cdb we just wrote.
:read
(
set /p input=
set /p input2=
)<settings.cdb
So, we just piped the first two lines of information that you wrote at the beginning of the file (which you have the option to skip setting the lines to check to make sure it's working) to set the variables of input and input2.
echo %input%
echo %input2%
pause
if %input%==1 goto newecho
pause
exit
:newecho
echo If you can see this, good job!
pause
exit
This displays the information that was set while settings.cdb was piped into the parenthesis. As an extra good-job motivator, pressing enter and setting the default values which we set earlier as "1" will return a good job message.
Using the bracket pipes goes both ways, and is much easier than setting the "FOR" stuff. :)
So you just have to do this right?:
#echo off
echo text shizzle
echo.
echo pause^>nul (press enter)
pause>nul
REM writing to file
(
echo XD
echo LOL
)>settings.cdb
cls
REM setting the variables out of the file
(
set /p input=
set /p input2=
)<settings.cdb
cls
REM echo'ing the variables
echo variables:
echo %input%
echo %input2%
pause>nul
if %input%==XD goto newecho
DEL settings.cdb
exit
:newecho
cls
echo If you can see this, good job!
DEL settings.cdb
pause>nul
exit
:: savevars.bat
:: Use $ to prefix any important variable to save it for future runs.
#ECHO OFF
SETLOCAL
REM Load variables
IF EXIST config.txt FOR /F "delims=" %%A IN (config.txt) DO SET "%%A"
REM Change variables
IF NOT DEFINED $RunCount (
SET $RunCount=1
) ELSE SET /A $RunCount+=1
REM Display variables
SET $
REM Save variables
SET $>config.txt
ENDLOCAL
PAUSE
EXIT /B
Output:
$RunCount=1
$RunCount=2
$RunCount=3
The technique outlined above can also be used to share variables among multiple batch files.
Source: http://www.incodesystems.com/products/batchfi1.htm
Kinda old subject but I had same question a few days ago and I came up with another idea (maybe someone will still find it usefull)
For example you can make a config.bat with different subjects (family, size, color, animals) and apply them individually in any order anywhere you want in your batch scripts:
#echo off
rem Empty the variable to be ready for label config_all
set config_all_selected=
rem Go to the label with the parameter you selected
goto :config_%1
REM This next line is just to go to end of file
REM in case that the parameter %1 is not set
goto :end
REM next label is to jump here and get all variables to be set
:config_all
set config_all_selected=1
:config_family
set mother=Mary
set father=John
set sister=Anna
rem This next line is to skip going to end if config_all label was selected as parameter
if not "%config_all_selected%"=="1" goto :end
:config_test
set "test_parameter_all=2nd set: The 'all' parameter WAS used before this echo"
if not "%config_all_selected%"=="1" goto :end
:config_size
set width=20
set height=40
if not "%config_all_selected%"=="1" goto :end
:config_color
set first_color=blue
set second_color=green
if not "%config_all_selected%"=="1" goto :end
:config_animals
set dog=Max
set cat=Miau
if not "%config_all_selected%"=="1" goto :end
:end
After that, you can use it anywhere by calling fully with 'call config.bat all' or calling only parts of it (see example bellow)
The idea in here is that sometimes is more handy when you have the option not to call everything at once. Some variables maybe you don't want to be called yet so you can call them later.
Example test.bat
#echo off
rem This is added just to test the all parameter
set "test_parameter_all=1st set: The 'all' parameter was NOT used before this echo"
call config.bat size
echo My birthday present had a width of %width% and a height of %height%
call config.bat family
call config.bat animals
echo Yesterday %father% and %mother% surprised %sister% with a cat named %cat%
echo Her brother wanted the dog %dog%
rem This shows you if the 'all' parameter was or not used (just for testing)
echo %test_parameter_all%
call config.bat color
echo His lucky color is %first_color% even if %second_color% is also nice.
echo.
pause
Hope it helps the way others help me in here with their answers.
A short version of the above:
config.bat
#echo off
set config_all_selected=
goto :config_%1
goto :end
:config_all
set config_all_selected=1
:config_family
set mother=Mary
set father=John
set daughter=Anna
if not "%config_all_selected%"=="1" goto :end
:config_size
set width=20
set height=40
if not "%config_all_selected%"=="1" goto :end
:end
test.bat
#echo off
call config.bat size
echo My birthday present had a width of %width% and a height of %height%
call config.bat family
echo %father% and %mother% have a daughter named %daughter%
echo.
pause
Good day.
The best option according to me is to have key/value pairs file as it could be read from other scripting languages.
Other thing is I would prefer to have an option for comments in the values file - which can be easy achieved with eol option in for /f command.
Here's the example
values file:
;;;;;; file with example values ;;;;;;;;
;; Will be processed by a .bat file
;; ';' can be used for commenting a line
First_Value=value001
;;Do not let spaces arround the equal sign
;; As this makes the processing much easier
;; and reliable
Second_Value=%First_Value%_test
;;as call set will be used in reading script
;; refering another variables will be possible.
Third_Value=Something
;;; end
Reading script:
#echo off
:::::::::::::::::::::::::::::
set "VALUES_FILE=E:\scripts\example.values"
:::::::::::::::::::::::::::::
FOR /F "usebackq eol=; tokens=* delims=" %%# in (
"%VALUES_FILE%"
) do (
call set "%%#"
)
echo %First_Value% -- %Second_Value% -- %Third_Value%
While trying to use the method with excutable configuration
I noticed that it may work or may NOT work
depending on where in the script is located the call:
call config.cmd
I know it doesn't make any sens, but for me it's a fact.
When "call config.cmd" is located at the top of the
script, it works, but if further in the script it doesn't.
By doesn't work, I mean the variable are not set un the calling script.
Very very strange !!!!