Tuesday, January 9, 2018

Windows batch string comparison in if statement wrong

Windows batch string comparison in if statement wrong



I try to write a small batch file that will react differently depending on a parameter. Unfortunately the "if" statement whch does not seem to work as expected in the form of 2 strings:

  @echo will close if "% 1" == "" Goto use echo " Parameter:% 1 "IF / I"% 1 "==" START "{echo" Why the hell is% 1 == starting? " SET parameter = geto start: and if / i "% 1" == "pause" {SET parameter = stop goto: execute} echo "parameter% 1% invalid": use echo "synapsis:" echo "switchservices Start "Stop" Goto: EOF: SET SERVICEMENME = DNSSH SET file name = Dnscache.exe Call:% parameter% _Service goto: EOF: stop_Service Echo ... stop service ... net stop% servicename% if (ERRORLEVEL GTR 0) Call: Kill_Service sc config% servicename% start = disabled service closed. Exit / b: kill_Service taskkill / f / im% filename% exit / b: Start_Service Echo ... Starting Service ... sc config% servicename% start = auto net start% servicename% exit / b: EOF  

The result is:

< Pre> c: users kollenba documents temp> KapschServices.cmd xxx "Parameter: xxx" "Why the hell is XXX == Start? "... the service is starting ... [SC] ChangeServiceConfig ERFOLG Der Enfford Denst Wurdes Rate Gateartate.

I do not understand why the condition

  If / I "% 1" == "Start"  

does any sign that does not work as expected?

Precondition: Batch File "Net Start "command must be executed with administrator permissions to allow. Used OS: Windows 7

We in batch If not, do not use the statement if the statement, instead of the parenthesis, then replace it:

  IF / I "% 1" == "START" {echo "hell why% 1 == is starting  == "START" (echo "why hell is% 1 = start?" Set parameter = start geto: execute) if / I "% 1" == "STOP" (SET parameter = stop goto: Exic)  

You stopped the service before echo .

And a bonus tip: You do not really need to label : EOF because goto: EOF always gets you at the end of the file Will



go to link download
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.