2

我在读取批处理脚本中的 CSV 文件内容时遇到问题。我有一系列文件说My_A_File.csv, My_B_File.csv ... My_Z_File.csv。我面临的问题是读取其中的特殊字符。因此,想要读取延迟扩展关闭的值。

当我用 disabledelayedexpansion 读取块中的值时,它们是空的!我该如何处理?

脚本:

@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
    )
)

输出:

Outer-My_A_File.csv
Outer-A
Inner-
Inner-
4

2 回答 2

3

这是延迟扩展应该如何工作的。但是您可以访问变量,CALL但这会影响性能(请注意,您不能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
    )
)

或者你可以使用管道。这也会影响性能(现在你可以使用 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
    )
)
于 2016-03-23T16:56:14.103 回答
3
于 2016-03-23T20:54:38.210 回答