Page 1 of 1

BATCH IF is failing with syntax error

Posted: 23 Feb 2015 04:08
by mskavim
What is wrong in the below syntax

Code: Select all

IF %EXPORT_TYPE%=="PART"

is failing with syntax error... any help is greatly appreciated....

Code: Select all

IF NOT DEFINED ITEM_ID( SET /P ITEM_ID="Enter ITEM_ID:" )
IF NOT DEFINED REVISION_ID( SET /P REVISION_ID="Enter REVISION_ID:" )
IF NOT DEFINED ITEM_NAME( SET /P ITEM_NAME="Enter ITEM_NAME ( NOT MANDOTORY \E):" )
IF NOT DEFINED EXPORT_TYPE( SET /P EXPORT_TYPE="Enter EXPORT_TYPE ( PART (or) ECO ):" )
IF %EXPORT_TYPE%=="PART"(
   SET EXCEL_RPT_FILE_NAME=ESI_%EXPORT_TYPE%_REPORT.xlsx
   ECHO %EXCEL_RPT_FILE_NAME%    
   IF NOT DEFINED REVISION_RULE( SET /P REVISION_RULE="Enter REVISION_RULE:" )   
) ELSE (
   SET EXCEL_RPT_FILE_NAME=PL_%EXPORT_TYPE%_REPORT.xlsx
)


Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 04:31
by ShadowThief
The lines above that line have parentheses inside of parentheses, which generally doesn't play nice in batch. You can escape them by putting a ^ in from of each ( and ).

For your if statement, the right side has quotes but the left side does not. Both sides need quotes in order for the condition to be evaluated properly.

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 05:46
by foxidrive
mskavim wrote:What is wrong in the below syntax

Code: Select all

IF %EXPORT_TYPE%=="PART"

is failing with syntax error... any help is greatly appreciated....


Maybe a little clearer: Use this and don't wrap the term being entered in double quotes:

Code: Select all

IF /i "%EXPORT_TYPE%"=="PART"

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 07:28
by Squashman
I would add the case insensitive option to your comparison.

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 07:49
by foxidrive
That's a very sensible plan Squashman, I added it to my post above yours.

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 10:39
by mskavim
@Squashman I tried that option but still I have the same issue... this is my complete script....


Code: Select all

@ECHO OFF
@BREAK ON
SETLOCAL ENABLEDELAYEDEXPANSION
COLOR 3

::--- START:TIME ---
SET starttime=%time%
SET _my_datetime=%date%_%time%
::-------------------------------------------------------------
::-   
::-   
::   -------------------------------------
::
::
::-------------------------------------------------------------
:: JAVA HOME
SET JRE_HOME=CHANGE_ME
:: TEAMCENTER VARIABLES
SET TC_ROOT=E:\APPS\Siemens\TC_ROOT
SET TC_DATA=E:\APPS\Siemens\TC_DATA

::#----------------------------------------------------------------------#
:: COMMAND DEBUG PRINT ( TRUE | FALSE )
::--------------------
:: SETTING VALE TO "TRUE" WILL IMPACT PERFORMACE HEAVILY!!!
::#----------------------------------------------------------------------#

:: CURRENT DIRECTORY NAME
FOR %%a IN (.) DO SET ROOT_FOLDER=%%~na
REM SET OUT_DIR=%~dp0

:: Set TITLE NAME...
SET JOB_TITLE=Executing:{ %ROOT_FOLDER% }
TITLE %JOB_TITLE%
REM COLOR 0e

::CLEAR SCREAN
CLS
::GET ROOT PATH
set ROOT_PATH=%CD%
::ECHO %ROOT_PATH%

::REQUIRED ENVIORMENT VARIABLES
IF "%JRE_HOME%"=="CHANGE_ME"( SET JRE_HOME="Enter JRE_HOME:" )
IF "%TC_ROOT%"=="CHANGE_ME"( SET /P TC_ROOT="Enter TC_ROOT:" )
IF "%TC_DATA%"=="CHANGE_ME"( SET /P TC_DATA="Enter TC_DATA:" )
CALL %TC_DATA%\TC_PROFILEVARS.BAT

ECHO #--------------------------------------------------------#
ECHO #       VENDOR PKG - USER ARGUMENTS                  #
ECHO #--------------------------------------------------------#
::GET TEAMCENTER CREDENTIALS
IF NOT DEFINED TC_USER( SET /P TC_USER="Enter TC_USER_ID:" )
IF NOT DEFINED TC_PASSWD( %ROOT_PATH%\BIN\EditV64.exe -m -p "Enter TC_USER_PASSWORD: " TC_PASSWD )
IF NOT DEFINED TC_GROUP( SET /P TC_GROUP="Enter TC_USER_GROUP:" )
IF NOT DEFINED ITEM_ID( SET /P ITEM_ID="Enter ITEM_ID:" )
IF NOT DEFINED REVISION_ID( SET /P REVISION_ID="Enter REVISION_ID:" )
IF NOT DEFINED ITEM_NAME( SET /P ITEM_NAME="Enter ITEM_NAME ^( NOT MANDOTORY \E ^):" )
IF NOT DEFINED EXPORT_TYPE( SET /P EXPORT_TYPE="Enter EXPORT_TYPE ( BOM (or) ECO ):" )
IF %EXPORT_TYPE%=="BOM"(
   SET EXCEL_RPT_FILE_NAME=ESI_%EXPORT_TYPE%_REPORT.xlsx
   ECHO %EXCEL_RPT_FILE_NAME%    
   IF NOT DEFINED REVISION_RULE( SET /P REVISION_RULE="Enter REVISION_RULE:" )
   IF NOT DEFINED BOM_LEVEL( SET /P BOM_LEVEL="Enter BOM_LEVEL( ALL (or) Valid Numeric Level Value ):" )
) ELSE (
   SET EXCEL_RPT_FILE_NAME=ESI_%EXPORT_TYPE%_REPORT.xlsx
)
IF NOT DEFINED EXPORT_VENDOR( SET /P EXPORT_VENDOR="Enter EXPORT_VENDOR ( YES (or) NO ):" )
IF NOT DEFINED OUT_DIR( SET /P OUT_DIR="Enter OUTPUT_DIRECTORY:" )

SET OBJ_NAME=%ITEM_ID%\%REVISION_ID%-%ITEM_NAME%
SET REPORT_TYPE=%EXPORT_TYPE% REPORT
ECHO #=================== START:: %ROOT_FOLDER% ========================#
IF  IF /I %EXPORT_TYPE%=="BOM"(
   IF /I %EXPORT_VENDOR%=="YES"(
   
     ECHO EXPORT BOM WITH VENDOR
     REM CALL:VENDOR_PKG_ITK "EXPORT BOM WITH VENDOR"
    
     REM CALL:GENERATE_EXCEL_REPORT %OUT_DIR% "BOM_COLUMN_CONFIG1"
    
   )  ELSE IF %EXPORT_VENDOR%=="NO"(
   
     ECHO EXPORT BOM WITHOUT VENDOR
     REM CALL:VENDOR_PKG_ITK "EXPORT BOM WITHOUT VENDOR"
    
     REM CALL:GENERATE_EXCEL_REPORT %OUT_DIR% "BOM_COLUMN_CONFIG2"
    
   ) ELSE (
      ECHO INVALID ARGUMENT VALUE FOR EXPORT VENDOR TYPE..!!!
      ECHO VALUE CAN EITHER YES/NO...
   )
) ELSE  IF /I %EXPORT_TYPE%=="ECO" (
    IF /I %EXPORT_VENDOR%=="YES"(
      
      ECHO EXPORT ECO WITH VENDOR
      REM CALL:VENDOR_PKG_ITK "EXPORT ECO WITH VENDOR"
      
      REM CALL:GENERATE_EXCEL_REPORT %OUT_DIR% "ECO_COLUMN_CONFIG1"
      
   ) ELSE  IF /I %EXPORT_VENDOR%=="NO"(
   
       ECHO EXPORT ECO WITHOUT VENDOR
       REM CALL:VENDOR_PKG_ITK "EXPORT ECO WITHOUT VENDOR"
      
       REM CALL:GENERATE_EXCEL_REPORT %OUT_DIR% "ECO_COLUMN_CONFIG2"
      
      
   ) ELSE (
      ECHO INVALID ARGUMENT VALUE FOR EXPORT VENDOR TYPE..!!!
      ECHO VALUE CAN EITHER YES/NO...
   )
) ELSE (
   ECHO INVALID ARGUMENT VALUE FOR EXPORT TYPE..!!!
   ECHO VALUE CAN EITHER BOM/ECO...
)



ECHO #=================== END:: %ROOT_FOLDER% ==========================#
::--- END:TIME ---
ECHO Start Time=%starttime% - End Time=%time%
::---
REM CMD

:endext
ENDLOCAL
set EXITVALUE=%ERRORLEVEL%
exit /B %EXITVALUE%

:GENERATE_EXCEL_REPORT
SETLOCAL ENABLEDELAYEDEXPANSION
   ECHO.
      ECHO   START   :   GENERATE_EXCEL_REPORT
      ECHO   MOUDLE      :   %~1
      ECHO   CUROUTDIR   :   %~2      
      ECHO   COL_NAMES   :   %~3
         SET COL_NAMES="%~3"
         FOR /F "delims=" %%i IN ('dir %CUROUTDIR% /b /ad-h /t:c /od') DO SET OUTDIR=%%i
         ECHO %OUTDIR%
         REM %JRE_HOME%\bin\java -cp "%ROOT_PATH%\bin\SPExcelProject.jar;%ROOT_PATH%/lib/*" com.splm.excel.report.ExcelReportMain -reportType=%RPT_TYP% -revRule=%REVISION_RULE% -propFile=%ROOT_PATH%\CreateReport.properties -colNames=%COL_NAMES% -objName=%OBJ_NAME% -reportFile="%OUTPUT_DIRECTORY%/./VENDOR_PKG_RPT.txt" -oFile="%OUTPUT_DIRECTORY%/./%EXCEL_RPT_FILE_NAME%
         SET STR=%STR:WGO_DistributionListFile_=%
      ECHO   _END   :   GENERATE_EXCEL_REPORT
   ECHO.
ENDLOCAL & SET %~3=%STR%
GOTO:EOF


:VENDOR_PKG_ITK
SETLOCAL ENABLEDELAYEDEXPANSION
   ECHO.
      ECHO   START   :   VENDOR_PKG_ITK
      ECHO   MOUDLE      :   %~1         
         IF /I "%~1"=="BOM" (
            %ROOT_PATH%\bin\VendorPkgExtraction.exe -u=%TC_USER% -p=%TC_PASSWD% -g=%TC_GROUP% -item_id=%ITEM_ID% -rev_id=%REVISION_ID% -rev_rule=%REVISION_RULE% -export_vendor=%EXPORT_VENDOR% -bom_level=%BOM_LEVEL% -accepted_bvrs="view,MBOM" -prefered_bvr=MBOM -out_dir=%OUTPUT_DIRECTORY% -log=%ROOT_PATH%\bin\log\ESIVendorPkgExtraction
         ) ELSE (
            %ROOT_PATH%\bin\VendorPkgExtraction.exe -u=%TC_USER% -p=%TC_PASSWD% -g=%TC_GROUP% -item_id=%ITEM_ID% -rev_id=%REVISION_ID% -export_vendor=%EXPORT_VENDOR% -out_dir=%OUTPUT_DIRECTORY% -log=%ROOT_PATH%\bin\log\ESIVendorPkgExtraction
         )         
      ECHO   _END   :   VENDOR_PKG_ITK
   ECHO.
ENDLOCAL & SET %~3=%STR%
GOTO:EOF


Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 10:52
by penpen
mskavim wrote:What is wrong in the below syntax

Code: Select all

IF %EXPORT_TYPE%=="PART"

is failing with syntax error... any help is greatly appreciated....
Nothing is wrong with this syntax, but you didn't use it; instead you have used this:
mskavim wrote:

Code: Select all

IF %EXPORT_TYPE%=="PART"(
Although it looks the same it is different:
The brackets are valid characters for the comparators, so the second comparator equals: "PART"(
=> there is no statement
=> syntax error

So always use a whitespace character between the second comparator and the statement:

Code: Select all

@echo off
set "ITEM_ID=IID"
IF NOT DEFINED ITEM_ID( echo not defined: "ITEM_ID("  )
IF NOT DEFINED ITEM_ID ( echo not defined: "ITEM_ID"  )
The internal brackets should be ok, because they are encapsulated in doublequotes.

The same is true for all other if blocks.

penpen

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 11:12
by Squashman
Still needs quotes around the left side if he is going to use them on the right side and should use /I for the comparison because you don't know what case the user is going to enter.

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 11:15
by Squashman
Why did you obfuscate the variable you were checking for. It should be BOM not PART!!!!! Really chaps my you know what!

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 11:53
by mskavim
I removed all the brackets like you guys suggested.... but still IF condition is failing...... I printed the "%EXPORT_TYPE%" before the IF check and the value is BOM... hmmm...i am close to give up....

Code: Select all

SET /P TC_USER="Enter TC_USER_ID :"
%ROOT_PATH%\BIN\EditV64.exe -m -p "Enter TC_USER_PASSWORD : " TC_PASSWD
SET /P TC_GROUP="Enter TC_USER_GROUP :"
SET /P ITEM_ID="Enter ITEM_ID :"
SET /P REVISION_ID="Enter REVISION_ID :"
SET /P ITEM_NAME="Enter ITEM_NAME, NOT MANDATORY :"
SET /P EXPORT_TYPE="Enter EXPORT_TYPE BOM or ECO :"
ECHO %EXPORT_TYPE%
IF /I %EXPORT_TYPE%=="BOM"(
      ECHO .
      SET EXCEL_RPT_FILE_NAME=ESI_%EXPORT_TYPE%_REPORT.xlsx
      ECHO %EXCEL_RPT_FILE_NAME%    
      SET /P REVISION_RULE="Enter REVISION_RULE :"
      SET /P BOM_LEVEL="Enter BOM_LEVEL, ALL or Valid Numeric Level Value :"
   ) ELSE (
      ECHO .
      SET EXCEL_RPT_FILE_NAME=ESI_%EXPORT_TYPE%_REPORT.xlsx
   )
SET /P EXPORT_VENDOR="Enter EXPORT_VENDOR YES or NO :"
SET /P OUT_DIR="Enter OUTPUT_DIRECTORY :"


Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 11:58
by penpen
As suggested in my above post: Add a space between "BOM" and (.
And as ShadowThief, foxidrive and Squashman suggest: Add doublequotes around the first comparator.

Code: Select all

IF /I "%EXPORT_TYPE%"=="BOM" (


penpen

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 12:28
by mskavim
@penpen ... my apologies .... I tried with "" but missed out that little info of giving space between "BOM" and "(" .... I am so use to using, actually spoiled by IDE for C++ and Java where i don't have to worry about the syntax.... bat is real test to me ... I am using notepad :)

I just found EasyShell and WickedShell plugin that can be used with eclipse for batch development ... hope it is going to make difference....

Thanks ALL!

Re: BATCH IF is failing with syntax error

Posted: 23 Feb 2015 12:31
by Squashman
I just use Notepad++.