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
Related
I'm trying to make a code where I can go to websites without going to my browser I have atop search function which I will change manually I'm fairly new to coding so any advice is helpful here is the code.
#echo off
echo Top searches
echo 1. Faceit
set /p name =
if %name% EQU "1" goto F
if %name% NEQ "1" then goto custom
:F
start "" https://www.faceit.com/en
:custom
echo What website would you like to go to?
set /p x =
start "" https://www.%x%
There's quite a bit going on in your code that is keeping it from working in any meaningful sort of way. Just at an initial glance, I see ten separate things that are either completely wrong or simply violate what could be considered "good programming practices" in batch.
1/2. Whitespace in variable names is significant
For some reason, Microsoft decided to allow whitespace in variable names, so %this is a valid variable name%. Seriously. As a result of this, both of your set /p statements are creating variables that you never use.
Instead of set /p name = and set /p x =, use set /p name= and set /p x=
3/4. Put quotes around set statements
This one is just good programming practice and is arguably not "wrong," but it's a good habit to form early.
Use quotes to avoid the user entering things like & or > and having those break the flow of the script. You can put the quotes to the around the prompt (like set /p variable="Enter text: "), but if you do that with a regular set statement, the quotes will become part of the value. To avoid this, put the first quote to the left of the variable name, like this: set /p "variable=Enter text: "
This also prevents any hidden spaces from getting tacked on at the end of the value by accident.
5. Then is not a keyword in batch
The then in your second if statement if going to give you a syntax error because it's not a valid keyword in batch. Just get rid of it.
if "%name%" NEQ "1" goto custom
6/7. Quotes in comparisons are significant
When you put quotes around one side of a comparison, you need to put quotes on the other side as well. This has the added effect of keeping characters like & and > from breaking the flow of the script.
if "%name%" EQU "1" goto F
if "%name%" NEQ "1" goto custom
8. A missing goto (or exit) will cause :custom to run immediately after :F
Batch scripts run from top to bottom unless acted upon by a goto, call, if, or some other flow control command. In this case, after start "" https://www.faceit.com/en is called, the very next non-whitespace line is :custom.
To avoid :custom from running, kill the script after the first start with exit /b or goto :eof - both of these will stop the script but keep the command prompt open if you ran the script from the command line instead of double-clicking it. Note that if you use goto :eof, you do not need to make a :eof label, since it's built into the command prompt.
9/10. Put colons in front of labels in goto commands
Again, not necessary, just good programming practice. You have to include the colons when you use call to run subroutines anyway, so you might as well be consistent everywhere.
Other notes
When the script is first run, all you see is
Top searches
1. Faceit
and that's it. Nothing to tell the user what to do or to indicate that they can enter another site by typing something other than 1. Unless you plan on being the only person to use the script, I'd recommend putting something somewhat more descriptive in that section.
If you're going to automatically tack on https://www. to the start of a custom URL, put that on the screen so that the user doesn't accidentally end up going to https://www.https://www.google.com or something.
You may want to look into the choice command for future versions of the script to replace the initial set /p command, depending on how many options you want to give the user.
Putting comments in your code wouldn't hurt.
Ultimately, it will look something like this
#echo off
echo Top searches
echo 1. Faceit
echo Enter anything else to go to a different site
set /p "name=Your selection: "
if "%name%" EQU "1" goto :F
if "%name%" NEQ "1" goto :custom
:F
start "" https://www.faceit.com/en
exit /b
:custom
echo What website would you like to go to?
set /p "x=https://www."
start "" https://www.%x%
I have a little program and when i run "if exist" it closes, can somebody tell me why? I have checked for syntax errors and none (i think), i will paste my code here and see what you can do.
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
set /p YesNoIn="[y/n]: "
if "%YesNoIn%"=="y" goto:yes
goto:menu
:yes
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done!
set /p yesnol="Would you like to launch MCEdit now? [y/n]: "
if "%yesnol%"="y" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
)
Right after you type the drive it closes, any help?
The issue that causes the script to exit early is a syntax error in the last if-statement of your else branch. If you run the script from the command line (not by double clicking on it) you'll get the error message:
="y" was unexpected at this time.
Indeed your last if-statement is:
if "%yesnol%"="y" (
but the cmd parser expects a double "=" sign (==) for comparisons, so you should have:
if "%yesnol%"=="y" (
The reason why it will see it even if it won't take the else branch is because an if-block (actually, all block of codes delimited by ( ... )) is parsed as if it was one single command written on one line (with each "subcommand" in your block separated with &&). As the parser will process that whole "line" in one go, it will detect whatever syntax error present inside the whole block.
Besides that syntax error, there are some little mistakes you've made in your script. The first one is that it is actually not good to declare labels inside a block of code. You've declared the :yes label inside the if-block in the else branch. A goto inside an if-block will "destroy" the if-context. You maybe won't notice in your code, but consider this example:
#echo off
set var=Let's brake the if statement
IF DEFINED var (
echo The variable var exists
IF "%var%"=="Let's brake the if statement" goto :expected
echo The variable var is not what I would expect though.
echo You have to change it to something else...
goto :outside
:expected
echo I'm happy with its value. It is at least something I would expect
echo This is the end of the if-branch
) ELSE (
echo Strange ... the variable var is not defined.
echo Indeed: var="%var%"
)
:outside
rem just to get outside
You would expect an output like
The variable var exists
I'm happy with its value. It is at least something I would expect
This is the end of the if-branch
but the output will be
The variable var exists
I'm happy with its value. It is at least something I would expect
This is the end of the if-branch
Strange ... the variable var is not defined.
Indeed: var="Let's brake the if statement"
The goto destroyed the if-context. As said earlier, the cmd parser will parse the whole if-block as one command. See it this way: you're asking the parser to abandon the command it was processing (the whole if block with the condition it just checked) and jump to somewhere else. That somewhere else is after the if condition, so it won't even evaluate that one again. So once you're inside a block (espacially if-blocks and for-loops) don't use goto to ignore some piece of code inside that block, use if-statements and put the code to ignore inside the if-block. A goto to jump outside a block of code is not a problem but from the moment the label is inside an if block for example, it can result in unexpected results.
The second mistake is about the variables YesNoIn and yesnol. As I said the whole if block is parsed in one go as one single command. It is not possible to give a variable a new value, and read that new value in the same command with the simple variable expansion %YesNoIn% or %yesnol%. A solution to this problem is delayed expansion (the link also has an example). As I was writing this answer, I saw #Josefz already posted an answer with delayed expansion so I won't repeat it here. But what I would recommend is that you take a look at the choice command. Using the choice command, you won't need delayed expansion. It just sets the errorlevel and there exists a way to check the errorlevel without worrying about delayed expansion: IF ERRORLEVEL n will check if the errorlevel is greater or equal to n. On top of that, choice automatically verifies if the user entered a correct value!
Your script with choice instead of set /p will look like this:
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
choice /c YN
REM Y ==> errorlevel = 1 ; N ==> errorlevel = 2
if ERRORLEVEL 2 goto:menu
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done!
choice /c YN /m "Would you like to launch MCEdit now? "
if NOT ERRORLEVEL 2 (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
)
PS: No Enter key needs to be pressed when choice is used.
EDIT: A third mistake is the choice of the variable APPDATA, it is already used by your windows OS in a user's context as you can see here.
You have simple typo in comparison operator in the latter if. Correct to if "%yesnol%"=="y"
I'm not sure whether changing APPDATA variable is a good idea.
Missing :menu label in your code.
Important: read and apply http://ss64.com/nt/delayedexpansion.html.
Never use :label nor :: label-like comment inside a command block enclosed in () parentheses
SETLOCAL EnableExtensions EnableDelayedExpansion
:::
:menu
:::
echo What drive would you like to launch MCEdit on?
set /p DRIVE="Drive: "
if exist "%DRIVE%:\MC\DATA\mcedit\mcedit.exe" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
) else (
echo You do not have MCEdit installed on this drive, would you like to install it?
set /p YesNoIn="[y/n]: "
if /I "!YesNoIn!"=="y" (
echo Choose the "mcedit.exe" file from a current installation on your pc.
pause
call "%DRIVE%\MC\DATA\FileViewer.bat"
xcopy "%OutCD%" "%DRIVE%:\MC\DATA\mcedit" /E
echo Done^!
set /p yesnol="Would you like to launch MCEdit now? [y/n]: "
if /I "!yesnol!"="y" (
set APPDATA=%DRIVE%:\MC\DATA
start %DRIVE%:\MC\DATA\mcedit\mcedit.exe
)
) else goto:menu
)
I wanted to make a basic text editor in batch and I'm using set /p to get user input and write that to a file.
When I type in something like "hello" or "hello " it works fine, but as soon as I write something with a space and another character after than, like "hello world", the window closes.
My code was:
set /p append=Write what you want to append:
Then I tried:
set /p "append=Write what you want to append: "
which i found online, but it didn't work.
pause isn't of any help because the program crashes as soon as I press enter.
Help!
Here is the full code:
:append
set /p "append=Write what you want to append: "
if %append%==return (
cls
goto start
)
echo %append% >> %filename%
goto append
:override
cls
echo Name: %filename%
set /p override="Write new content: "
echo %override% > %filename%
pause
cls
goto start
:writefile
echo.
set /p filename=Write file name.extension:
choice /c AO /m "Append to file or override? "
if %ERRORLEVEL%==1 (
cls
echo Name: %filename%
goto append
) else (
goto override
)
Whenever the user of a batch file is prompted for a string using set /P the user has the freedom to
enter nothing at all by pressing simply just RETURN or ENTER which results in the environment variable is still not defined after prompt if it was not defined before, or the variable keeps its current value if it was already defined before, or
enter really anything including a string which could result in an exit of batch execution because of a syntax error somewhere in batch code later or an unwanted behavior.
Let us look on the line
if %append%==return (
If environment variable is not defined before with a default value and the batch user enters nothing, this line expands during preprocessing step to:
if ==return (
This command line is of course invalid and results in an exit of batch processing because of a syntax error.
Now let us assume the user enters the string:
I want to append this string.
Then the IF command line expands to:
if I want to append this string.==return (
And of course also this command line is invalid.
The other answerers suggested to enclose the two strings to compare in double quotes, i.e. use the command line:
if "%append%"=="return" (
But does that really solve all possible issues?
Is the code now really safe against any user input.
Let us look what happens if the user enters the string:
" is a double quote. It should be used around paths like "%ProgramFiles(x86)%".
The IF command line expands now to:
if "" is a double quote. It should be used around paths like "%ProgramFiles(x86)%""=="return" (
And this command line is again invalid and results in an exit of the batch file because of a syntax error.
So how to make batch code safe against really any user input?
The solution is using delayed environment variable expansion wherever the user entered string is referenced.
Example:
#echo off
setlocal EnableDelayedExpansion
echo.
echo Enter EXIT to exit this batch script.
:PromptUser
echo.
set "UserInput=Nothing^!"
set /P "UserInput=Please enter a string: "
if /I "!UserInput!" == "exit" goto EndBatch
echo You entered: !UserInput!
goto PromptUser
:EndBatch
echo.
echo Thanks for running this example batch code.
echo.
echo The batch file ends in 3 seconds ...
endlocal
%SystemRoot%\System32\ping.exe localhost -n 4 >nul
By using delayed expansion the user entered string can't result in a invalid or unexpected command line in later code. This is also important for the command line
echo !append!>>%filename%
It is important to have no space character left of >> or this space character is also written into the file as trailing space.
But delayed expansion is also important here in case of user enters for example just 2 which would result with echo %append%>>%filename% in
echo 2>>%filename%
which does not append the character 2 to the file, but would append STDERR to the file which results in an empty line written to the file.
Delayed expansion is also needed for this string entered by the user:
(Var1 > 0x2F) && (Var1 < 0x3A)
which should be written with ECHO into the file as entered and not what Windows command interpreter would produce after expanding the string when using echo %append%>>%filename%.
Change your set to put quotes in the right place around the prompt string, and your if line to put quotes around the string being tested.
set /p append="Write what you want: "
set append
echo xx%append%yy
if "%append%"=="return" (
echo Yes!
)
Your problem is with
if %append%==return (
which will give a syntax error if append contains spaces.
use
if "%append%"=="return" (
"quoting a string" makes cmd interpret it as a single element so the required syntax of if,
if string==string (
is no longer violated.
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
I'm really new in this forum so I hope to respect all your rules, if not please forgive me!
I've just started studying something about batch files and I'm trying to execute a simple program from batch passing a parameter (the last aim is to submit a SAS program passing a date parameter).
Is it possible to activate a sort of list where I can choose some between pre-defined parameters?
--> This is the real aim of my work
I'm trying to "play" with this code:
#echo off
title Setting up execution period
echo Insert your date in the format GGMMMAAAA (es: '31DEC2003'D).
SET /p data_par=Insert the date to filter datas:
SET first_byte=%data_par:~1,1%
if "%first_byte%"=="" (
GOTO tag1
) else (
GOTO tag2
)
:tag1
msg * Missing value
:tag2
msg * Well done!
pause
I've tried in a lot of ways but it looks like the IF statement is not executed, I don't know where am I wrong.
Another question: why the prompt closes after i press "Enter" (afte the set/p command is executed)?
--> this has been resolved putting the "pause" command at the end of the script.
Thank you all for the attention,
Best regards!
Squotty
Put a pause at the end of your code to see the errormessages.
correct syntax for if when using else is:
if "a"=="b" (dosomething) else (dosemethingelse)
You can write it in several lines, but there are rules, where to set the paratheses:
if "a"=="b" (
echo this is code for something
rem more lines possible
) else (
echo this is code for something else
rem more lines possible
)
The first ( has to be on the same line than if.
) else ( have to be on one line.
If you press just enter with set /p, the variable remains unchanged (propably empty), so your code will go on with the code and hits the line else. Here it will tell you "else is not recognized as a command..."
at your tagx you should tell batch, where to stop execution. Use goto :eof to stop execution or goto somewhere to continue somewhere else. If you don't, it will just continue with the next lines.
Example:
:tag1
msg * Missing value
goto :eof
:tag2
msg * Well done!
goto :continue
pause
:continue
REM go on with the program...
(note: the pause will never be reached. I let it there to show you, how things work)
EDIT instead of just checking for some input you can check for the correct format:
echo %data_par%|findstr /r "[0-3][0-9][A-Z][A-Z][A-Z][1-2]0[0-9][0-9]">nul && (
echo correct format
goto continue
) || (
echo wrong format
goto startover
)
It's not bullet proof (eg. 38ABC2019 would be considered "correct"), but at least it checks for the correct format (e.g. 15.12.2019 or 12/15/2014 would be "not correct")
#ECHO OFF
SETLOCAL
SET "item1=date1"
SET "item2=date2"
SET "item3=date3"
SET "item4=date4"
FOR /l %%a IN (1,1,4) DO CALL ECHO(%%a. %%item%%a%%
ECHO(U. User-specified
choice /c 1234u
CALL SET selection=%%item%errorlevel%%%
IF NOT DEFINED selection (
SET /p selection="Your date-selection ? "
)
IF NOT DEFINED selection ECHO No selection made&GOTO :EOF
ECHO selection is %selection%
GOTO :EOF
This code may be of assistance.
It's normal to develop batch code using a batch window. Simply set up a shortcut to command prompt (Start>Programs>Accessories) which would allow you to run the script over and over and retain the results on-screen without using 'pause'. Editing can be accomplished by using notepad batchfilename.bat from the prompt (if you are using notepad for an editor - if using something better, then substitute that program's name). You can exit from the batch window by executing an exit command.
You can also get help on batch commands by using commandname /? - it's often cryptic and there are plenty of quirks. Extensive help available here on SO.