if statements inside command - windows

I am trying to make a if statement for a CLI i am making in batch. this is the code
if "%command%" == "browser"(
echo Warning, this will only change the default broswer for this session
echo What would you like the default browser to be changed to?
set /p browserdefault=
)
I want it to check "browserdefault" and see if it is chrome, edge or a diffrent broswer
if it is none of those it should say "error, invalid broswer."
is there any way for there to be a if statement inside of the already existing if statement? like if %browserdefault% is "chrome" start C:%username%\filepath\tochrome?
I tried many diffrent ways but none of them seems to work

As pointed out in the other answer this is Windows batch. I do not know a way of nested if statements.
I would use call statements to get rid of the nested if statements like this:
:getCommand
set /P command=Enter command:
call :%command%
goto :getCommand
:browser
set /P browserdefault=Enter browser:
if not "%browserdefault%"=="chrome" if not "%browserdefault%"=="edge" echo error, invalid broswer.
goto :eof

What you are trying to do is right, noble and good. However, this is Windows batch. The ordinary rules of style & good taste do not apply.
You should not try to nest other statements inside a batch command. There are times this will appear to work, but (based on a number of factors, such as a poorly considered logical condition, or even different input strings!) will occasionally cause the block to fail miserably. This will set you up for a world of painful debugging.
The suggestion in the comments to search for menu is a good one. This should lead you to code that will have (what looks like) many unnecessary & unrelated blocks, all sprinkled with horrible gotos. Just... it's fine. This is Windows batch. It will work, it will be reliable. Try not to think about the style too much.
There are some example menus in this excellent Q&A:
Menus in Batch File
(as noted in at least one answer there, when using the naive if ERRORLEVEL 1, this will actually check if the number is equal OR greater than which requires starting with a higher ERRORLEVEL and working down to IF ERRORLEVEL 0, with a GOTO jump for each check -- using a statement with a % variable such as if "1" == "%ERRORLEVEL%" or similar avoids this pitfall).

Related

Stopping an executable running if vbs loop if outside criteria is met

I'm using a VBScript to write into a configuration file config.txt, then run an executable SomeName.exe with the configuration I set.
The excecutable is not mine, I cannot interact with it.
The result is written in another text file Result.txt.
In the end, it looks like this
Set objShell = CreateObject("WScript.Shell")
For MyParameter = mystart to myend
'here I overwrite conf.txt with MyParameter value
Rt = objShell.run(SomeName.exe, 1, True) '--> True means "wait until the end before processing"
Next
What I'd like to do is to check the result.txt file and, if it's ok, stop the .exe and resume the loop with next value of MyParameter.
I already know how to read the file and decide whether the result is good enough or not (basically I read the last line and compare it with something else, very easy stuff) with a second script.
What I don't know is how to make this two scripts work together.
For now the only way I have is to run manually my second script and make it check from time to time (with Sleep function) if the result is good. In that case, I use taskill /im "SomeName.exe". But it's quite ugly and I have to run it with an infinite loop since I don't know how long it will take to SomeName.exe to reach the result (it's a simulation, it can be very very long !).
Have you got any idea on how to do that ?
Thanks a lot in advance for any help you can give me,
Why use WaitOnReturn=True?
Change that to False and then add your script code loop that checks for final line right after it.

Is there something I'm doing wrong to pick up this error?

I am new to Ti-basic, and I am trying to code it. I'm trying to make this 'special type of input' program. It is kind of like input, but it will show the word as it is being pressed (and there is no need to put in alpha)
Here is the code so far that I believe is pertaining to the error
:{41,42,43,51,52,53,54,55,61,62,63,64,65,71,72,73,74,75,81,82,83,84,85,91,92,93,94,102,103,103}→∟KEYS
:"ABCDEFGHIJKLMNOPQRSTUVWXYZθ :?"→Str7
:0→K
:""→Str1
:
:Repeat K=105
:getKey→K
:If max(∟KEYS-K)
:prgmFINDIND
:.........
:End
Inside prgmFINDIND, This is the code
:1+sum(not(cumSum(∟KEYS=K)))→I
://I is used later on in the code. It isn't pertaining to the problem.
I have done some testing with pause on this already, and I found the problem was in the if statement. It returns an 'INVALID DIM' error.
Anybody know what's wrong?
In this part (edited a bit)
Repeat K=105
getKey->K
If max(|LKEYS=K
prgmFINDIND
Str1+sub(Str7,I,1->Str1
End
prgmFINDIND is only called if the key that was pressed is in the list, otherwise the index I is not changed (and possibly implicitly zero, or whatever other value that was left there).
Pressing GOTO on the INVALID DIM actually goes to Str1+sub(Str7,I,1->Str1, indicating that a bad index was used to index into Str7.
It could be fixed by using an If/Then block, which can cover more than one statement:
Repeat K=105
getKey->K
If max(|LKEYS=K
Then
prgmFINDIND
Str1+sub(Str7,I,1)->Str1
End
End

VBScript Nothing returns garbage

We have to use VBScript as an embedded script engine in our Hospital Information System.
When we use nothing to set the value of a control (textbox/checkbox/...) it worked always fine. Since somepoint it sets now the textbox to "?>".
item("TEXTBOX").value = nothing ' Leads to -> "?>"
It is not completly clear what causes this, maybe a windows update is responsible, every rollup ~ since KB3212646 Win7 2017-01 seems to cause this error.
My Question is now, has someone else also seem this error, so that it is clear that MS causes this error or is our HIS publisher responsible for not handling nothing correct.
I know setting a textbox to Nothing is not best practice instead "" should be better, but since the item object could be more the just a textbox e.g. a combobox/checkbox this seems, from an objectoriented perpsective, better. Or am I completly wrong?
Following #Ansgar comment, you should apparently change everywhere you have = nothing to = "" in your example
item("TEXTBOX").value = ""
Beware to keep the nothing if you have the Set keyword in left
Set some_object = Nothing

Batch script the same on both PC's but not working?

Hello I have 2 batch files, it works perfectly on one machine but not so perfect on another.
Here is the code.
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "%ln:&="&set "%"
set realuser=%user:"=%
echo %realuser%
So on my machine it shows like this:
echo Liam
Liam
On the other machine it shows like this:
echo "=
"=
It's the exact same machines only difference is one is running Windows 8 (working) and the other windows 7 ("=)
EDIT:
Thank you all for the answers, I managed to solve this by editing the way the userl.txt file is generated to make it display just the name, e.g "Liam" without quotes. Then use this
set /p user=
That seems to work for what I need s there is only 1 value ever going to be in that file.
Thank you all!
A simple echo %thisvariabledoesnoexist:"=% will show the same result.
The reason for the observed output is that the variable %user%, that seems that have to been assigned a value in the set "%ln:&="&set "%" line, did not get any value.
The problem is probably that the input line does not contain the required value or the format of the input line is different from the expected one.
As MC ND has pointed out, the critical point is that variable user must be defined to work properly. If it is not defined, then you get your problem result.
I don't see how identical user1.txt files being processed on two machines with identical batch scripts can possibly give different results as you describe.
Strike that, I can think of one way, but it is a long shot. The assumption is the first two lines are supposed to define the user variable, perhaps along with other variables. But suppose the first two lines do not define the user variable on either machine. Perhaps user is already defined on the machine that "works" before the script is even run, and it is not defined on the other machine. That is the only thing I can come up with that would yield the result you describe.
I do see one thing that concerns me in your code. The following line of code implies that sometimes you get quotes in your input.
set realuser=%user:"=%
You state that quotes in your value interfere, so you remove them. But you are removing them too late! The prior line may not set all the values properly if there are quotes in the value of ln.
Try the following:
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "ln=%ln:"=%"
set "%ln:&="&set "%"
echo %user%

is there a way to stop the popping up of the md5.exe tool while it generates the hash for a file

i have written a program in vbscript for which i have used md5.exe to generate hash. since there are many files to which the hash has to be generated, the md5 hash repeatedly generates hash for each file one after the other. but while this process is in progress, i can see it popping out on the screen as it generates the hashes ( it does not pop out the hashes, the tool itself pops out on the screen repeatedly). i want to do something such that it stops popping out yet generate the hash for all the files. please help guys!
Generally, when running a command, youj can supress its output by directing it to a file, then deleting the file
md5.exe blahblah >null the >null being the critical part. Note however, that null doesn't directly it to a magical blackhole. It creates a file named null and prints the output there
This would supress the output. IF you posted some code, I could have told you how to do that there. but if you are running it as a shell exec, this should work
You can use the run method to supress the window.
Set WshShell = CreateObject("WScript.Shell")
cmd= "C:\Users\Administrator\desktop\experimenting\md5.exe"
'OR whatvere your whole command is
cmdRun = WshShell.Run(cmd,0,true);
Answer from here
Want to hide command prompt window in using WshShell.Exec method
(Upvote the guy if you find it useful)
Please note that you have tro use the output file to read. You cannot read the output from the shell anymore.

Resources