batch-file - CMD 从文件中获取字符串并将其设置为变量以在 cd 中使用

标签 batch-file cmd

我是批处理文件的新手,我正在尝试编写一个文件来完成我的部分工作(我知道很懒)

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

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/

相关文章:

windows - 解析和重命名文本文件

xml - 通过linux脚本在Control-M企业管理器中自动上传批量Job XML

Windows 批处理脚本启动程序并退出控制台

windows - 如何在批处理中迭代两个列表

powershell - bat 文件禁止用户输入

if-statement - IF 的奇怪结果

vb.net - 从cmd命令中获取字符串?进程.StartInfo

windows - 在 Windows 7 命令行中更改 Putty session 的标题文本

vb.net - 从 PC 获取唯一的硬件 ID

windows - 从 .bat 启动独立进程