Frage

Scenario: I have a pre-commit batch script that checks for blank comments, and calls a vbscript file for authorization of the user. The vbscript file then exits by wscript.echo 1 or wscript.echo 0 after which the control comes back to the batch file to exit with success or failure.

@ECHO OFF
set REPOS=%1
set TXN_NAME=%2
set DEBUG=0
SET ThisScriptsDirectory=%~dp0
set svnlook = "C:\Program Files\TortoiseSVN\bin\"

REM: check for blank comment
for /f "tokens=*" %%a in ('%svnlook% svnlook author -t %TXN_NAME% %REPOS%') do set AUTH_NAME=%%a
%svnlook% svnlook log %REPOS% -t %TXN_NAME% | findstr .................... > nul  
 if %errorlevel% gtr 0 (goto err) else (goto noerr)

 :err  
 echo. 1>&2  
 echo Your commit has been blocked because you didn't enter a comment. 1>&2  
 echo Write a log message describing the changes made and try again. 1>&2
 echo Thanks 1>&2
 exit 1

 :noerr 
  for /f %%i in ('cscript.exe //nologo %ThisScriptsDirectory%pre-commiting.vbs %REPOS% %TXN_NAME% %ThisScriptsDirectory% %AUTH_NAME%') do set vars = %%i  
  if %vars% == "0" ( goto success) else (goto failure)   
   :failure
    echo. 1>&2  
    echo You do not have the permissions to work on this repository. 1>&2  
      echo Request modifications access from support team. 1>&2
      echo Thanks 1>&2
      exit 1

   :success
    echo Commit Authorized...
    exit 0

I tried doing an echo of my vbscript to a file and it seems to return 1 or 0 which is what I am expecting it to do..

Thanks in advance. . .

War es hilfreich?

Lösung

The spaces in the set command are important, and included both in the value and in the name of the variable.

....
.... set "vars=%%i"

if operators will only work if the left and the right operands follow the same rules. If you quote one value, quote also the other. If not, one value will have quotes, the other not and condition will always evalueate to false

if "%vars%"=="0"
if %vars%==0

if "%vars%" equ "0"
if %vars% equ 0

Andere Tipps

Spaces are significant in SET assignments.

You have set vars = %%i, which defines a variable named "vars[space]", with a value of "[space]0" or "[space]1". Simply remove the spaces before and after the equal sign.

set vars=%%i

I used both the solutions given here..

I modified my loop where the set statement is used.

do set "vars=%%i"  

I modified my comparison operator by adding quotes on both the sides

 if "%vars%"=="0" ( goto success) else (goto failure)   

and the code ran like a knife on butter :)

+1 to both for a nice explanation..

Thanks, Venkat.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top