CMD从文件中获取字符串,并将其设置为可变的CD使用(CMD get string from fil

2019-09-22 16:27发布

我是新来的批处理文件,我试着写一个做我的工作的一部分(我知道偷懒右)

到目前为止,我有以下...

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.

任何帮助吗?

Answer 1:

你会想看看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变量在你的内部循环,而不是创建一个新的变量。



Answer 2:

@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


文章来源: CMD get string from file and SET it as a variable to use in cd