Batch file: Extracting substring from input parameter to use in IF statement

Posted by Neomoon on Stack Overflow See other posts from Stack Overflow or by Neomoon
Published on 2010-04-05T18:42:24Z Indexed on 2010/04/05 18:43 UTC
Read the original article Hit count: 295

Filed under:
|
|
|
|

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 ouput 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.

© Stack Overflow or respective owner

Related posts about batch

Related posts about substring