Goto inside if codeblock

Microsoft Windows xp professional w/sp2
May 4, 2010 at 02:12:10
Specs: Windows XP
Hello,
I have a problem using the goto inside the if code block; error:The syntax of the command is incorrect.

this is a part of a larger script I'm trying to write,

Usage of script
my.bat 0
or
my.bat c:\temp

my.bat
----------------------------------------------------------
@echo off

rem set patch dir
set nx_patch_dump_path=%~1

:chk_patch

if "%nx-patch-dump-path%"=="0" (
echo no patch to install...
goto eof
) else (
if not exist %nx-patch-dump-path% (
echo error! wrong path!
goto eof
)
goto install_patch
)


:install_patch
echo install patch...
goto eof

:eof
pause

any pointers appreciated


See More: Goto inside if codeblock

Report •


#1
May 4, 2010 at 02:17:20
When you call the program with : my.bat 0
Isn't this wrong then:

set nx_patch_dump_path=%~1

Secondly, if ever you have issues with a GOTO, and the name of the label is larger than 8 characters, limit it to 8 characters.

Thirdly (and most important), replace ECHO OFF with ECHO ON. You know, that is the purpose of that command. ECHO OFF is disabling debugging actually ...


Report •

#2
May 4, 2010 at 02:37:59
You don't need to put an eof label; it's built into XP.

But strangely, you DO need to use a colon.

goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#3
May 4, 2010 at 06:29:42
hi All, thanks for the ideas.
Tried all suggestions, but didn't work; see my try below

------------call this approach-1----------------------
@echo on

rem set patch dir
set nx_patch_dump_path=%~1

rem echo's ok, so %~1 is OK
echo nx_patch_dump_path=%nx_patch_dump_path%

:chk_patch
if "%nx-patch-dump-path%"=="0" (
echo no patch to install...
goto :eof
) else (
if not exist %nx-patch-dump-path% (
echo error, wrong path!
goto :eof
)
goto install_patch
)

:install_patch
echo install patch...
goto :eof

------------------------------
gives error-- The syntax of the command is incorrect.
C:\p\test-scripts> if not exist (
-----------------------------------------

so. I removed the complex "if code blocks" to achieve my objective see approach-2

-------approach-2---------

@echo off

rem set patch dir
set nx_patch_dump_path=%~1
echo nx_patch_dump_path=%nx_patch_dump_path%

:chk_patch

if not "%nx-patch-dump-path%"=="0" goto no_patch
if not exist %nx-patch-dump-path% goto error1
goto install_patch

rem -----------------------------
:error1
echo path does not exist..
goto :eof

:no_patch
echo no patch to install
goto :eof

:install_patch
echo install patch...
goto :eof


--------------
I would still be interested if someone can tweak approach-1 and make it work!
many thanks in adv


Report •

Related Solutions

#4
May 4, 2010 at 06:44:20
%nx_patch_dump_path% isn't set, so the IF EXIST statement fails.

Report •

#5
May 4, 2010 at 08:48:56
Here's the first chunk.

====================================

@echo off & setLocal EnableDELAYedeXpansion

set NX=%1

:chk_patch

if "!NX!"=="0" (
  echo no patch to install...
  goto :eof
) else (
    if not exist !NX! (
      echo error, wrong path!
      goto :eof
    )
    goto install_patch
  )

:install_patch
echo install patch...
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#6
May 4, 2010 at 09:04:49
Edit : indeedy ... empty variables, a very popular cause for errors all around

Report •

#7
May 4, 2010 at 10:08:26
thanks to all for the ideas, thanks to M2! your soln worked!


Report •

Ask Question