【问题标题】:Evaluate environment variable read from file in batch评估从文件中批量读取的环境变量
【发布时间】:2015-11-28 19:22:32
【问题描述】:

我通过脚本读入文件repos.txt

@echo off

for /F %%s in (%~dp0repos.txt) do (
    echo %%s
)

文件repos.txt 包含以下行:

%UserProfile%\Documents\Repository\A
%UserProfile%\Documents\Repository\B

echo 命令在不评估环境变量%UserProfile% 的情况下为我提供了目录。当我想将它与 git 一起使用时,这是一个问题。

如何评估环境变量?

我尝试了setlocal,就像完成了hereexclamation marks 一样。不幸的是,没有给出正确的输出。

【问题讨论】:

    标签: batch-file environment-variables


    【解决方案1】:

    命令 call 可用于在分配给环境变量时扩展环境变量,如下代码所示:

    @echo off
    setlocal EnableDelayedExpansion
    for /F "usebackq eol=| delims=" %%s in ("%~dp0repos.txt") do (
        set "RepositoryPath=%%s"
        echo Not expanded: !RepositoryPath!
        call set "RepositoryPath=%%s"
        echo And expanded: !RepositoryPath!
    )
    endlocal
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2021-08-28
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多