Regarding values taken in the Batch Script

August 8, 2011 at 04:52:55
Specs: Windows XP
I have written a batch file using for loop to count the number of lines.After counting the number of lines,i should check with the number using Relational Operators.While getting number of lines in IF condition to check,it its not taking value from currently executed FOR loop.Its taking value from previous execution of the same Batch Script.Can you tell me How to make it correct?

This is my batch script

Set Intr_National_Reject_file=D:\sample\opt\rejects.csv
IF Exist %Intr_National_Reject_file% (
for /f %%A in ('type "%Intr_National_Reject_file%"^|find "------" /c /v') do ( set /A Intr_National_Reject_count =%%A )
echo %Intr_National_Reject_count%
IF %Intr_National_Reject_count% GTR 1 (
echo Rows found in a f_commondity_position have product_category_bk
goto EOF
) ELSE (
echo Rows found in a f_commondity_position which does not have product_category_bk
goto EOF
)
) ELSE (
Echo %Intr_National_Reject_file% file not found
goto EOF
)
:EOF


Thanks in Advance :)


See More: Regarding values taken in the Batch Script

Report •

#1
August 8, 2011 at 15:25:13
Seems the IF command is inside parenthesis so you could/should use delayed expansion e.g. Setlocal enabledelayedexpansion

Enter Set /? at the Command Prompt for documentation

You could also try canceling the environment variable by setting it to blank prior to the second and subsequent execution of the script.

Good luck.


Please come back & tell us if your problem is resolved.


Report •

#2
August 8, 2011 at 23:06:36
Hi Wahine,

Thanks for your response.Am new in writing Batch Scripts.Can u describe more on it???


Report •

#3
August 9, 2011 at 01:35:41
Ok. This is just to give an example of the use of delayed expansion of environment variables, I cannot test your script in any way but suspect that some of the coding is questionable. I have amended some of your coding, note the usage of enabled delayed expansion and the ! character instead of % when the environment variable is inside parenthesis..

Good luck.

@echo off
cls
setlocal enabledelayedexpansion

Set Intr_National_Reject_file=D:\sample\opt\rejects.csv

IF not Exist %Intr_National_Reject_file% (
    echo !Intr_National_Reject_file! not found&&goto eof

    ) ELSE (

    for /f %%A in ('type "!Intr_National_Reject_file!"^|find "------" /c /v') do ( set /A Intr_National_Reject_count =%%A )
    echo !Intr_National_Reject_count!

    IF !Intr_National_Reject_count! GTR 1 (
       echo Rows found in a f_commondity_position have product_category_bk
       goto EOF

       ) ELSE (

       echo Rows found in a f_commondity_position which does not have product_category_bk
       goto EOF
       )
)
:EOF


Please come back & tell us if your problem is resolved.


Report •
Related Solutions


Ask Question