2

I am facing issue with reading contents of CSV files in batch script. I have a series of files say My_A_File.csv, My_B_File.csv ... My_Z_File.csv. The issue I was facing is reading special characters in them. Hence, wanted to read the values with delayedexpansion turned off.

When I read the values in the block with disabledelayedexpansion, they are empty! How can I handle this?

Script:

@echo off
setlocal enabledelayedexpansion
for /L %%g in (65,1,90) do (
    cmd /c exit /b %%g
    set codeval=!=ExitCodeAscii!
    set fileToReadFrom=My_!codeval!_File.csv

    if exist My_!codeval!_File.csv (
        echo Outer-!fileToReadFrom!
        echo Outer-!codeval!
        setlocal disabledelayedexpansion
        echo Inner-%fileToReadFrom%
        echo Inner-%codeval%
        endlocal
    )
)

Output:

Outer-My_A_File.csv
Outer-A
Inner-
Inner-
3
  • In the line set codeval=!=ExitCodeAscii!, there is a second = which I guess is not intended to be there; am I right? Commented Mar 23, 2016 at 19:54
  • 1
    @aschipfl this is a special variable that contains the exit code as ascii character Commented Mar 23, 2016 at 20:38
  • Wow, very interesting, @npocmaka, I was not aware of that! Commented Mar 23, 2016 at 22:18

2 Answers 2

3

This how the delayed expansion is supposed to work.However you can access the variables with CALL but this will the performance (mind that you cant CALL FOR ):

@echo off
setlocal enabledelayedexpansion
for /L %%g in (65,1,90) do (
    cmd /c exit /b %%g
    set codeval=!=ExitCodeAscii!
    set fileToReadFrom=My_!codeval!_File.csv

    if exist My_!codeval!_File.csv (
        echo Outer-!fileToReadFrom!
        echo Outer-!codeval!
        setlocal disabledelayedexpansion
          call echo Inner-%%fileToReadFrom%%
          call echo Inner-%%codeval%%
        endlocal
    )
)

or you can use pipes.Which also will hit the performance (now you can use break|for "usebackq" %%a in ("Inner-%%fileToReadFrom%%") do @echo %%~a):

@echo off
setlocal enabledelayedexpansion
for /L %%g in (65,1,90) do (
    cmd /c exit /b %%g
    set codeval=!=ExitCodeAscii!
    set fileToReadFrom=My_!codeval!_File.csv

    if exist My_!codeval!_File.csv (
        echo Outer-!fileToReadFrom!
        echo Outer-!codeval!
        setlocal disabledelayedexpansion
           break|echo Inner-%%fileToReadFrom%%
           break|echo Inner-%%codeval%%
        endlocal
    )
)
Sign up to request clarification or add additional context in comments.

Comments

3

Use a subroutine to process code with delayed expansion disabled as follows:

@echo off
rem skip subroutine code
goto :toMain

:toProcessDDE
  rem subroutine to process delayed expansion disabled
  setlocal disabledelayedexpansion
  echo Inner-%fileToReadFrom%
  echo Inner-%codeval%
  endlocal
exit /B

:toMain
setlocal enabledelayedexpansion
for /L %%g in (65,1,90) do (
    cmd /c exit /b %%g
    set codeval=!=ExitCodeAscii!
    set fileToReadFrom=My_!codeval!_File.csv

    if exist My_!codeval!_File.csv (
        echo Outer-!fileToReadFrom!
        echo Outer-!codeval!
        call :toProcessDDE
    )
)

Read

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.