批量评估从文件中读取的环境变量

Evaluate environment variable read from file in batch

我通过脚本读取了文件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 就完成了 here and the surrounding with exclamation marks。不幸的是,没有给出正确的输出。

命令 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