我尝试使用我在下面的for循环中设置的变量myVar重命名文件。问题是重命名不起作用。谁能告诉我为什么?
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2%myVar%.txt
答案 0 :(得分:3)
我敢打赌你的代码被放在里面的括号中就像这样:
if some == comparison (
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2%myVar%.txt
)
如果是这种情况,您需要使用延迟扩展来获取块中 modified 的变量的值:
setlocal EnableDelayedExpansion
if some == comparison (
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2!myVar!.txt
)
有关详细信息,请在此网站或其他网站上搜索“延迟扩展”。
答案 1 :(得分:1)
您好请尝试此代码。
for /F "tokens=*" %%i in (myfile.txt) do (
set %filename% = %%i
ren file1.txt file2%filename%.txt
)
答案 2 :(得分:0)
您的代码正在运行,可能问题在于tmpfile2.txt内容(令牌),
或者变量值可能有空格,这就是为什么不起作用的原因,
没有tmpfile2的内容我们无法知道为什么不起作用。
尝试这种方式来看看发生了什么:
For /F "delims= usebackq" %%# in ("tmpFile2.txt") do (Echo "myVar=%%#" & set "myVar=%%#")
Echo Rename "file1.txt" "file2%myVar%.txt"
Rename "file1.txt" "file2%myVar%.txt"
或者其他:
For /F "delims= usebackq" %%# in ("tmpFile2.txt") do (Rename "file1.txt" "file2%%#.txt")