2013-04-09 93 views
0

希望有人可以提供帮助。 我在DOS下工作(Windows 7中,如果它的事项),并得到了下面的代码小片段:剔除部分路径名

@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%),但我不知道为什么?

回答

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!提取的文件名。