Batch file 'for' loops - multiple lines

后端 未结 1 1158
孤街浪徒
孤街浪徒 2021-01-01 12:15

Why will the following lines work in a batch file?

 for  %%a in (\"C:\\Test\\*.txt\") do set FileName=%%~a
 echo Filename is: %FileName%

Bu

相关标签:
1条回答
  • 2021-01-01 12:37

    It is because everything between the parentheses is loaded as one line. So %FileName% is expanded (at load time) before it is set (at run time). If you need to use the second format, you need to enable delayed expansion. Then you will have difficulty if the filename contains a !. This would work if there are no parentheses in filenames.

     setlocal enabledelayedexpansion
     for  %%a in ("C:\Test\*.txt") do (
         set FileName=%%~a
         echo Filename is: !FileName!
     )
    
    0 讨论(0)
提交回复
热议问题