0

I am working on this python script which is to be executed using BAT file. The Batch file takes 3 parameters. If the user gives no parameters ( NULL ) while executing BAT file, the code should assign values to the 3 variables. ( repo_path, py_file_path, branch )

How does IF ELSE condition work in BAT? I tried if %abc%=="" ( ) it did not work!! In fact the IF condition is failing.

Below is my code.

cls
set repo_path=%1
set py_file_path=%2
set branch=%3

IF [%repo_path%] == [] GOTO assign
:assign
set repo_path="C:\GIT\Analysis\360-analysis"
set py_file_path="C:\GIT\Analysis\360-analysis\NJ\WIP-Sai\Auto_Git_Project\Python_Files"
set branch="master"

echo %repo_path%
cd %repo_path%
python %py_file_path%\Auto_Git_Pull.py %branch%
pause
8
  • Does this answer your question? Comparing two strings in batch Commented May 11, 2020 at 20:07
  • The expression if %abc%=="" actually compares string with "string"... Commented May 11, 2020 at 20:09
  • For each individual variable within a script you would generally use this syntax, If Not Defined repo_path set "repo_path=C:\GIT\Analysis\360-analysis". For the purposed of this exercise however, you could probably use If "%~3" == "" GoTo … Commented May 11, 2020 at 20:25
  • Instead of == you should use EQU, it might work. As for null values, there really isn't a specific null operator in batch. I would just create another variable with blank content and call it null. There is technically a null variable which is nul as used in >nul to silence outputs, but it cannot be used in comparisons like if. Commented May 11, 2020 at 20:27
  • 1
    Create a new empty file. Type in echo %1. On the next line, type if "%1"=="" echo Empty. Save the file as stringtest.bat. Run it from the command prompt as stringtest 1, and you'll see that it outputs 1 and nothing else. Run it again with stringtest and nothing else, and you'll see ECHO is ON followed by Empty. Commented May 11, 2020 at 20:49

1 Answer 1

1

Here's an example based upon my comment.

@Echo Off
Set "repo_path=%~1"
Set "py_file_path=%~2"
Set "branch=%~3"

If Not Defined repo_path (
    Set "repo_path=C:\GIT\Analysis\360-analysis"
    Set "py_file_path=C:\GIT\Analysis\360-analysis\NJ\WIP-Sai\Auto_Git_Project\Python_Files"
    Set "branch=master"
)
Echo %repo_path%
CD /D "%repo_path%" 2>NUL || GoTo :EOF
"F:\ullPath To\python.exe" "%py_file_path%\Auto_Git_Pull.py" "%branch%"
Pause

Please note, that in addition to the above example, you should really incorporate some methodology to check whether the input was only one or two arguments, or more than three, (as opposed to just none or at least three). Also you should validate those entries, just in case the second argument was provided, but doesn't exist, and the same for the third argument. The example above will already close the script if the first argument isn't valid, (using the code on line 12).

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

3 Comments

CD /D "%repo_path%" 2>NUL || GoTo :EOF what does this mean? Also I am closer than ever. I will test and respond. please hang on
@SaiAstro, change directory to the expanded value for the defined variable named repo_path, redirect any error from doing so to the NUL device, (so that it isn't visible), and if the CD command is unsuccessful, i.e. the location doesn't exist, exit the script, (GoTo :EOF).
you beautiful SOB! (sry but love u) i finally got it .. had to modify a bit but works! :)

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.