@ECHO OFF
echo x:\junk\main\something\file1.txt > temp.txt
echo x:\junk\main\something\file2.txt >> temp.txt
echo x:\junk\main\else\file3.txt >> temp.txt
set TMP=temp.txt
setlocal enabledelayedexpansion
for /F %%I IN (%TMP%) DO (
echo File is: %%I
set CFile=%%I
set CFile=%CFile:~13%
echo File really is: %CFile%
)
运行此命令会生成以下结果:
C:\temp>test
File is: x:\junk\main\something\file1.txt
File really is:
File is: x:\junk\main\something\file2.txt
File really is:
File is: x:\junk\main\else\file3.txt
File really is:
C:\temp>
那么......我做错了什么?为什么不将值保存到CFile变量中? 它似乎与FOR循环或%% I语法(与%I%相反)有关,但我不确定为什么?
答案 0 :(得分:0)
您正在使用延迟命令行扩展,因此您需要在某些地方使用!
字符,而不是%
。
@ECHO OFF
echo x:\junk\main\something\file1.txt > temp.txt
echo x:\junk\main\something\file2.txt >> temp.txt
echo x:\junk\main\else\file3.txt >> temp.txt
set TMP=temp.txt
setlocal enabledelayedexpansion
for /F %%I IN (%TMP%) DO (
echo File is: %%I
set CFile=%%I
set CFile=!CFile:~13!
echo File really is: !CFile!
echo.
set CRealFile=%%~nxI
echo The filename is: !CRealFile!
)
虽然,我不确定你真的想使用!CFile:~13!
。上面修改过的代码不再丢失CFile
值,但看起来不太合适。我添加了几行来演示如何从%%I
提取文件名为!CRealFile!
。