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-
James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • In the line `set codeval=!=ExitCodeAscii!`, there is a second `=` which I guess is not intended to be there; am I right? – aschipfl Mar 23 '16 at 19:54
  • 1
    @aschipfl this is a [special variable](http://ss64.com/nt/syntax-variables.html) that contains the exit code as ascii character – npocmaka Mar 23 '16 at 20:38
  • Wow, very interesting, @npocmaka, I was not aware of that! – aschipfl Mar 23 '16 at 22:18

2 Answers2

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
    )
)
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
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

JosefZ
  • 28,460
  • 5
  • 44
  • 83