windows batch files: setting variable in for loop

后端 未结 3 1691
夕颜
夕颜 2020-12-01 06:37

I have a number of files with the same naming scheme. As a sample, four files are called \"num_001_001.txt\", \"num_002_001.txt\", \"num_002_002.txt\", \"num_002_003.txt\"

3条回答
  •  醉梦人生
    2020-12-01 07:02

    I'm not sure whether this is officially documented, but you can simulate delayed expansion using the call statement:

    @echo off
    FOR /R %%X IN (*.txt) DO (
      set temp=%%~nX
      call echo directory %%temp:~0,7%%
    )
    

    Doubling the percent signs defers the variable substitution to the second evaluation. However, delayed expansion is much more straightforward.

提交回复
热议问题