Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Win/Batch: If then else related questionHi there,  (Read 8735 times)

0 Members and 1 Guest are viewing this topic.

lwkt

    Topic Starter


    Rookie

    Win/Batch: If then else related questionHi there,
    « on: August 19, 2009, 08:46:52 AM »
    Hi there,

    I am going to use Dos/Batch with VBscript suggested from some guru in this forum to generate time stamp for text files. The partial codes are as follow: -

    @echo off
    IF EXIST "collect_*.TXT" (
    echo Wscript.echo eval(WScript.Arguments(0))>"%cd%\evaluate.vbs"
    For /f %%D in ( ' cscript //nologo "%cd%\evaluate.vbs" "day(date)"   ' ) do set /a d=%%D
    For /f %%M in ( ' cscript //nologo "%cd%\evaluate.vbs" "month(date)" ' ) do set /a m=%%M
    For /f %%Y in ( ' cscript //nologo "%cd%\evaluate.vbs" "year(date)"  ' ) do set /a yyyy=%%Y
    del/q "%cd%\evaluate.vbs"
    if %d% LSS 10 (set dd=0%d%) else (set dd=%d%)
    if %m% LSS 10 (set mm=0%m%) else (set mm=%m%)
    set TimeStamp=%yyyy%-%mm%-%dd
    ) else (
    echo.
    echo no information collected!
    )
    echo.
    pause
    exit

    However, the program does not run properly with following error mesages:

    It gives an error message: ) was unexpected at this time.
    for statement:
    echo Wscript.echo eval(WScript.Arguments(0))>"%cd%\evaluate.vbs"

    and it gives error message: 10 was unexpected at this time.
    for the statements: if %d% LSS 10 (set dd=0%d%) else (set dd=%d%)
                             if %m% LSS 10 (set mm=0%m%) else (set mm=%m%)

    Any idea about that?


    Thanks,
    Thomas

    gpl



      Apprentice
    • Thanked: 27
      Re: Win/Batch: If then else related questionHi there,
      « Reply #1 on: August 19, 2009, 09:32:10 AM »
      I suspect it does not like nested IFs
      I would fake it with GoTos

      Code: [Select]
      @echo off
      IF Not EXIST "collect_*.TXT" GoTo IfElse

      echo Wscript.echo eval(WScript.Arguments(0))>"%cd%\evaluate.vbs"
      For /f %%D in ( ' cscript //nologo "%cd%\evaluate.vbs" "day(date)"   ' ) do set /a d=%%D
      For /f %%M in ( ' cscript //nologo "%cd%\evaluate.vbs" "month(date)" ' ) do set /a m=%%M
      For /f %%Y in ( ' cscript //nologo "%cd%\evaluate.vbs" "year(date)"  ' ) do set /a yyyy=%%Y
      del/q "%cd%\evaluate.vbs"
      if %d% LSS 10 (set dd=0%d%) else (set dd=%d%)
      if %m% LSS 10 (set mm=0%m%) else (set mm=%m%)
      set TimeStamp=%yyyy%-%mm%-%dd
      GoTo IfEnd

      :IfElse
      echo.
      echo no information collected!
      GoTo IfEnd

      :IfEnd
      echo.
      pause
      exit


      Salmon Trout

      • Guest
      Re: Win/Batch: If then else related questionHi there,
      « Reply #2 on: August 19, 2009, 10:23:26 AM »
      I suspect it does not like nested IFs
      I would fake it with GoTos

      I wrote the evaluate.vbs script some months ago, under a different screen name.

      Anyhow, you are partly right. Use of this parenthetical structure:

      IF EXIST "collect_*.TXT" (
      blah blah blah
      ) else (
      blah blah blah
      )

      means that

      1. the parentheses in this statement...

      echo Wscript.echo eval(WScript.Arguments(0))>"%cd%\evaluate.vbs"

      and these...

      cscript //nologo "%cd%\evaluate.vbs" "day(date)"
      cscript //nologo "%cd%\evaluate.vbs" "month(date)"
      cscript //nologo "%cd%\evaluate.vbs" "year(date)"

      ...need escaping, ^(with carets^) and...

      2. without delayed expansion the variables d, m and yyyy will be set to blanks and thus result in "not expected at this time" messages when you do LSS GTR EQU etc comparisons

      So the idea of using gotos is a good one.

      Here is my version

      Code: [Select]
      @echo off

      IF not EXIST "collect_*.TXT" goto noinfo

      echo Wscript.echo eval(WScript.Arguments(0))>"%cd%\evaluate.vbs"
      For /f %%D in ( ' cscript //nologo "%cd%\evaluate.vbs" "day(date)"   ' ) do set /a d=%%D
      For /f %%M in ( ' cscript //nologo "%cd%\evaluate.vbs" "month(date)" ' ) do set /a m=%%M
      For /f %%Y in ( ' cscript //nologo "%cd%\evaluate.vbs" "year(date)"  ' ) do set /a yyyy=%%Y
      del/q "%cd%\evaluate.vbs"
      if %d% LSS 10 (set dd=0%d%) else (set dd=%d%)
      if %m% LSS 10 (set mm=0%m%) else (set mm=%m%)
      set TimeStamp=%yyyy%-%mm%-%dd

      :noinfo
      echo.
      echo no information collected!

      echo.
      pause
      exit