I've been trying to compare my computername to some pre-set string. From reading around on google, namely http://commandwindows.com/batchfiles-branching.htm, I've attempted the following and many variants of the same line with /I
, "%ComputerName"
, A513242
etc
IF (%ComputerName% == "A513242") (
EXIT) ELSE (
ECHO "else taken")
where "A513242" is the result of calling ECHO %ComputerName%
this seems to always take the "else taken" branch.
Any help as to why the (EXIT
) case is not being taken/ what syntactical mistake I am making would be appreciated.
2) You should use equals() method to compare String variables if you want to check equality, this is the right way of comparing String in Java. String overrides equals() from Object and changes it's default behavior which was same as the == operator.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.
You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.
Try this:
if "%ComputerName%"=="A513242" (exit) else (echo "else taken")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With