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
)
Related
I need to do an script that looks for some text in a file and also look for a file itself in a %userprofile% path, and it works fine but when i tried to unify it so that instead of outputing 2 confirming messages only output 1, something is not going as expected, this is the code:
hostname > hstnm.txt
SET /p hstnm=<hstnm.txt
SET pccer=.filextension1
SET pcsip=.filextension2
SET fullhost=%hstnm%%pccer%
SET fullsip=%hstnm%%pcsip%
SET fullroute=%userprofile%\thepath\%fullhost%
SET siproute=%userprofile%\thepath\%fullsip%
FINDSTR /m "<Protocol>TLS</Protocol>" "%siproute%"
IF %errorlevel%==0 (
SET siptest="true"
) ELSE (
SET siptest="false"
)
IF EXIST "%fullroute%" (
SET certest="true"
) ELSE (
SET certest="false"
)
IF %siptest%=="true" & %certest%=="true" (
ECHO message if everything good
) ELSE (
ECHO message if something bad
)
pause
The FINDSTR and the IF EXIST are working properly (if u swap SET for an ECHO it does display a message). The problem arrives at the part of using the %certest% and the %siproute%, it is doing nothing, just outputs this (if i quit the #echo off)
...
C:\path>IF EXIST "C:\Users\Administrador\Appdata\Roaming\Interactive Intelligence\PureCloud Softphone\certificates\sip\GSSES0401107C.grupogss.corp.cer" (SET certest="true" ) ELSE (SET certest="false" )
No se esperaba & en este momento.
C:\path>IF "true"=="true" & "true"=="true" (
C:\path>
Help me please! thanks in advance.
Sometimes we do not need to do parenthesized if's.
There is already a variable containing the hostname called %COMPUTERNAME% which you can use without having to create a temp file
Pre-set variables and only change them if the files do not exist instead of the if else statements.
nested if's do not use &. We simply put the next after the other. if one condition returns true, it will do the next, etc. if any condition fails, it will stop processing the rest of the line and continue to the next line:
#echo off
set "pccer=.filextension1"
set "pcsip=.filextension2"
set "fullhost=%COMPUTERNAME%%pccer%"
set "fullsip=%COMPUTERNAME%%pcsip%"
set "fullroute=%userprofile%\thepath\%fullhost%"
set "siproute=%userprofile%\thepath\%fullsip%"
set "siptest=false"
set "certest=false"
FINDSTR /m "<Protocol>TLS</Protocol>" "%siproute%"
if not errorlevel 1 set "siptest=true"
if exist "%fullroute%" set "certest=true"
if "%siptest%" == "true" if "%certest%" == "true" echo message if everything good & goto :done
echo message if something bad
:done
pause
The line in question has a '&' character which I assume you are trying to use as a boolean AND operator - 'IF' in CMD does not have an AND operator. The CMD command chain mechanism does and that's where the probably confusion arises. I typically nest IFs in cases like this:
IF %siptest%=="true" (
IF %certest%=="true" (...
The '&' operator " simply chains a number of command together on one line:
dir & cd \ & dir & del myfile.txt
All the above command will be executed, one after the other.
On the other hand, the '&&' operator works a bit more like an AND operator in that it executes the next command in the chain only if ERRORLEVEL of the previous command is "0" (no error).
del myfile.txt && echo Done! && cd c:\
The echo command will only be executed if del command returns a 0 as its ERRORLEVEL. The cd command is dependent on the results all of the previous commands, meaning it only takes one failed command to break the chain.
I am personally also careful how I use variables and always enclose them:
IF "%MYVAR%"=="0" (...
I do that because in the end you are not comparing a variable to something, but rather it is a simple string comparison; CMD variables are replaced with the content of what they represent before any actions on them. When that replacement happens can be adjusted to a certain degree, but in the end it is always a string comparison. That means that if the variable doesn't have any content (it happens) and you use that to compare, without some kind of enclosure, then your script will fail and exit, due to the resulting IF being malformed.
https://ss64.com/nt/if.html
https://ss64.com/nt/syntax-conditional.html
I found a very strange behavior of batch script on Windows. Why does the first example crashes but not the others when pressing enter without anything?
This will crash if you just press enter without anything.
#echo off
set /p menuinput=
if "%menuinput%"=="" (
Echo just enter input
pause
) else if "%menuinput:~2,2%"==":\" (
echo A destination path has been input with quotations.
pause
) else (
echo anything else
pause
)
exit
But this will not crash even if you just press enter without anything.
#echo off
set /p menuinput=
if "%menuinput%"=="" (
Echo just enter input
pause
) else (
echo anything else
pause
)
exit
Also this will not crash even if you just press enter without anything.
#echo off
set menuinput=""
set /p menuinput=
if "%menuinput%"=="" (
Echo just enter input
pause
) else if "%menuinput:~2,2%"==":\" (
echo A destination path has been input with quotations.
pause
) else (
echo anything else
pause
)
exit
Actually, your third example is significant, so I've rolled it back.
The reason for the crash with the first example is that if menuinput is empty, then else if "%menuinput:~2,2%"==":\" ( will yield a syntax error. The second succeeds because you are not attempting to find a substring of an empty string.
The third - well, actually, this sets menuinput to "". This is not an empty string. Note that set /p will leave the variable unaltered if you simply press Enter
Batch parses the entire statement from if though to the very last closing parenthesis in the logical statement, so it finds a syntax error and barfs. Only if the statement is syntactically correct with the statement be executed (the same goes for a for statement, of course).
Had you used set "menuinput=" before the set /p then menuinput would have been "set" to nothing
Attempting to reliably parse a set /p-input value is fraught with problems.
So, to detect a just-Enter, set the variable to nothing, then execute the set /p and then use
if not defined menuinput goto noinput
Then attack the problem of the further analysis of the input-string.
Running a proper menu is often discussed - there are many items on SO - just use the search facility. (I'm mentioning this solely because of the variablename you used)
Also, you appear oto be using pause to try to catch the window before it closes. When you use the point-click-and-giggle method of executing a batch, the batch window will often close if a syntax-error is found. You should instead open a 'command prompt' and run your batch from there so that the window remains open and any error message will be displayed.
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 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 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.