0

This is my batch script to read csv file and assigning values to batch variables. This script reads the file perfectly, but when it comes to the last line of csv file...the last line is executed twice. I am not getting the reason why the last line is read twice. Following is my batch code

@echo off

:: Set input file in _variable
Set _InputFile=%1

:: Store input line into different _variables
FOR /F "tokens=1-4* delims=," %%A IN (%_InputFile%) DO (
    Set _var1=%%A
    Set _var2=%%B
    Set _var3=%%C
    Set _var4=%%D
CALL :PROCESS
)

:PROCESS
IF "%_var1%"=="Application Name" echo FIRST LINE
IF "%_var1%"=="Transition Portal" echo %_var2%
IF "%_var1%"=="Coast" echo %_var2%
IF "%_var1%"=="TI" echo %_var2%

This is my csv file:

Application Name,Environment,Release No.,Revision No.

Transition Portal,Test,,

Coast,Dev,handle,0

TI,Prod,check,3

The output I get is:

FIRST LINE

Test

Dev

Prod

Prod

If you notice 'Prod' got printed two times. Can anyone please let me know why is the last line executed two times?

1 Answer 1

1

You need to add the line goto :eof because you are falling through to the subroutine when done with the FOR loop.

CALL :PROCESS
)
GOTO :eof

You should also add GOTO :eof as the last line of :PROCESS. Yes, it works now, but if someone adds to your code later things will not work as expected.

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

Comments

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.