views:

448

answers:

1

This is a very basic example of what I am trying to implement in a more complex batch file. I would like to extract a substring from an input parameter (%1) and branch based on if the substring was found or not.

@echo off
SETLOCAL enableextensions enabledelayedexpansion

SET _testvariable=%1
SET _testvariable=%_testvariable:~4,3%

ECHO %_testvariable%

IF %_testvariable%=act CALL :SOME
IF NOT %_testvariable%=act CALL :ACTION

:SOME
ECHO Substring found
GOTO :END

:ACTION
ECHO Substring not found
GOTO :END
ENDLOCAL

:END

This is what my output looks like:

C:\>test someaction

act

=act was unexpected at this time.

If possible I would like to turn this in to a IF/ELSE statement and evaluate directly from %1. However I have not had success with either.

A: 

In your IF statements, replace = with ==.

I think you also want to replace your CALL statements with GOTO.

Here is your code, but using IF/ELSE instead of two IF statements.

@echo off
SETLOCAL enableextensions enabledelayedexpansion

SET _testvariable=%1
SET _testvariable=%_testvariable:~4,3%

ECHO %_testvariable%

IF %_testvariable%==act (
  GOTO :SOME
) ELSE (
  GOTO :ACTION
)

:SOME
ECHO Substring found
GOTO :END

:ACTION
ECHO Substring not found
GOTO :END

:END

ENDLOCAL
aphoria
It was the == that seemed to have fixed it. My original batch file where I was having the issues was ~300+ lines and I overlooked the minor detail.Thanks for being my extra set of eyes.
Neomoon
You are welcome. Batch files can be tricky to debug sometimes.
aphoria