0

I have this code snippet from a batch script. The script fails with:

" ) was unexpected at this time "

SETLOCAL EnableDelayedExpansion

set var="value "
    echo !var!
    IF "!var!"=="value " (
        echo Perfect )

P.S. I need the Delayed Expansion in my script.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Supratim Das
  • 203
  • 1
  • 3
  • 8
  • 1
    Be careful on assigning output of `wmic` to an environment variable as wmic output is in Unicode (UTF-16 Little Endian). See the answers on [How to correct variable overwriting misbehavior when parsing output](http://stackoverflow.com/questions/24961755/) for details on how to get output of `wmic` as ANSI string. – Mofi May 19 '16 at 09:37
  • Thanks for the advice :) Yes i am being careful on that part. Since you came up with this-- is there any way to redirect the undesired output of wmic command to NUL, that is i do not want the same to be shown in the console. Something like this: for /f "skip=1 tokens=7" %%f in ('wmic process where "name='java.exe'" get commandline') do ( set v=%%f ) In this very case if there are no spawning instances of 'java.exe' i see an output in the console- No Instance(s) Available That is correct, but i dont want the same to be displayed in the console. – Supratim Das May 20 '16 at 05:16

1 Answers1

0

The excerpt you've posted works without error messages.Though if you want to include the space at the end in the variable name you can use the quotes like this:

@echo off
SETLOCAL EnableDelayedExpansion

set "var=value "
    echo !var!
    IF "!var!" == "value " (
        echo Perfect )

edit based on the commends:

@echo off
setlocal enableDelayedExpansion
for /f "usebackq" %%f in (`"wmic process where name='java.exe' get commandline /format:value"`) do (
    call ::run  "%%~f"
)

:run
for /f "tokens=7" %%# in ("%%~1") do set "var=%%#"  
echo "!var!" 
IF "!var!"=="someValue " ( 
    echo Already running 
) Else (
 echo S

topped )

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • The space is mandatory. I am not able to paste the whole script here but the variable var is basically assigned with the output of a "wmic" command which includes the space. And as the IF loop is encountered i get the above error as mentioned. – Supratim Das May 19 '16 at 09:29
  • @SupratimDas - so you'll need to paste a bigger part of your code.May be your problems are caused by [this](http://www.dostips.com/forum/viewtopic.php?t=4266) – npocmaka May 19 '16 at 09:37
  • here it goes as below: for /f "skip=1 tokens=7" %%f in ('wmic process where "name='java.exe'" get commandline') do ( set v=%%f goto run ) :run set var=!v! echo "!var!" IF "!var!"=="someValue " ( echo Already running ) Else ( echo Stopped ) Here the output from the FOR loop is set to variable v which is basically the 7th token of the first line. That variable is stored in 'var' and used in an IF check. even if the IF check is true it does not execute the commands inside the IF loop. There are a whole lot of commands inside the IF loop, not a single statement. – Supratim Das May 19 '16 at 09:47
  • sorry i am new here. I was not able to put the code as it should look like. Its a bit clumsy – Supratim Das May 19 '16 at 09:48
  • I got this now..I separated parts of the code into different files and started appending the parts of the as and when it was working. Astonishingly i dont get the error now. :) – Supratim Das May 20 '16 at 05:21