我是新来的批处理文件,我试着写一个做我的工作的一部分(我知道偷懒右)
到目前为止,我有以下...
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批处理文件选项。 从上述的链接:
与for循环工作时延迟的变量扩充是非常有用。 通常情况下,FOR循环的整个评价为即使它跨越批处理脚本的多行的单个命令。
所以,你的脚本最终将看起来像这样:
@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
变量在你的内部循环,而不是创建一个新的变量。
@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 echo %%n>>c:result.txt