我是批处理文件的新手,我正在尝试编写一个文件来完成我的部分工作(我知道很懒)
到目前为止,我有以下...
SET skip=1
REM for all the directories indicated to contain core repositories
FOR /F "skip=%skip% delims=" %%i IN (C:\Repos.txt) DO (
SET TgtDir =%%i
echo %TgtDir% >> C:\result.txt
)
Repos.txt 的内容为:
60000
C:\somedir\someotherdir\
C:\a\b\c\
基本上我希望这个脚本通过一个文件,忽略稍后将用于延迟设置的第一行,然后提取每一行然后(理想情况下)将它传递给一个 cd 命令,但现在我只是想得到它到变量 TgtDir 中。
当我运行这个脚本时,C:\result.txt 中的输出是:
ECHO is on.
ECHO is on.
有什么帮助吗?
最佳答案
你会想看看 EnableDelayedExpansion批处理文件的选项。从上述链接:
Delayed variable expansion is often useful when working with FOR Loops. Normally, an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.
所以你的脚本最终会是这样的:
@echo off
setlocal enabledelayedexpansion
SET skip=1
REM for all the directories indicated to contain core repositories
FOR /F "skip=%skip% delims=" %%i IN (C:\Repos.txt) DO (
SET TgtDir=%%i
echo !TgtDir! >> C:\result.txt
)
作为替代方案,只需使用
%%i
内部循环中的变量,而不是创建一个新变量。
关于batch-file - CMD 从文件中获取字符串并将其设置为变量以在 cd 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513691/