0

I have a batch file that first checks an outside file to see the source branch for the build then runs an if else if statement that runs regex to match the source branch which then determines where the build will deploy:

@echo off
setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "usebackqdelims=" %%a in ("BuildBranch.txt") do (
  set currentline=%%a
  set filecontent=!filecontent!%SEPARATOR%!currentline!
)

echo %filecontent%

If NOT "%filecontent%"=="develop(.*)" (
    echo Deploying to dev DEVELOP
) else If NOT "%filecontent%"=="^.feature(.*)" (
    echo Deploying to dev FEATURE
) else If NOT "%filecontent%"=="master(.*)" (
    echo Deploying to integration MASTER
) else If NOT "%filecontent%"=="^.hotfix(.*)" (
    echo Deploying to integration HOTFIX
)

pause

I have run each of the regex's through the regex101 site but there may still be issues with them. My main issue is that ever time I run my batch file it only runs the first of the If statements. The variable %filecontent% is feature/MyNewFeature so it should see that it doesn't match the first if statement and go on to the second statement. I don't know if this is an issue with my regex or my if statements.

2
  • 1
    What shell do you use? CMD does not have "use regex to match" feature... (if /? to get help) Commented Dec 4, 2015 at 17:27
  • 1
    Ah that would probably be why the regex isn't working to well then, with my build I use Powershell. I guess I would need to use powershell first to do the regex then. Commented Dec 4, 2015 at 17:33

1 Answer 1

4

The IF does not support regex matching. But you could try some workaround like the following:

echo %filecontent% | >nul findstr /i /r /c:"develop.*" || ( echo Deploying to dev DEVELOP )
echo %filecontent% | >nul findstr /i /r /c:"^.feature.*" || ( echo Deploying to dev FEATURE )
echo %filecontent% | >nul findstr /i /r /c:"master.*" || ( echo Deploying to dev MASTER )
echo %filecontent% | >nul findstr /i /r /c:"^.hotfix.*" || ( echo Deploying to dev HOTFIX )

From there you could build around the findstr command, since it can match some regex (not advanced ones thou), in conjunction with the || operator, for cases of if NOT, or the && operator, for cases of if.

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

You missed the ^ at the .feature.* string; and since there is if NOT in the question, you should use || instead of &&... anyway, great approach!
Thanks for the hint. Edited it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.