如何扩展环境变量里面的字符串从文件读取? [英] How to expand environment variable inside string read from file?
问题描述
%USERPROFILE%
。 示例:
%USERPROFILE%\dir
C:\another\dir
然后我使用这段代码读取所有的行并连接到;
(可以使用cat和tr命令因为git):
set include_file = C:\path\to\file.txt
/ pre>
/ fdelims =%% i in('cat%include_file%^ | tr\\\\
;')do set include_content = %% i
这将创建一个变量,其中包含字符串
%USERPROFILE%\dir; C:\another\dir
通常运行echo%USERPROFILE%
会给你一些类似C:\Users\xxx\dir
但运行它与%include_content%
只是输出原始字符串而不展开%USERPATH%
我的最终计划是添加directo这样我的PATH就像这样
设置PATH =%include_content%;%PATH%
,但这不起作用,因为变量没有展开。
我的问题是我可以以某种方式扩展字符串中的变量,以便将真实的目录添加到路径中?
解决方案您可以使用
CALL
重新排序一行。
所以这应该做这个工作
call setinclude_content = %include_content%
for循环
I have a file with a list of paths. Some of them might include environment variables such as
%USERPROFILE%
.Example:
%USERPROFILE%\dir C:\another\dir
I'm then reading all the lines and concatenating with
;
using this code (the cat and tr commands are available because of git):set include_file=C:\path\to\file.txt for /f "delims=" %%i in ('cat %include_file% ^| tr "\\n" ";"') do set include_content=%%i
This creates a variable with the string
%USERPROFILE%\dir;C:\another\dir
Normally runningecho %USERPROFILE%
would give you something likeC:\Users\xxx\dir
but running it with%include_content%
just outputs the original string without expanding%USERPATH%
My final plan is to add the directories to my PATH like this
set PATH=%include_content%;%PATH%
but this doesn't work since the variable isn't expanded.My question is can I somehow expand the variables in the string so that the real directories get added to the path?
解决方案You can reparse a line by using
CALL
.So this should do the job
call set "include_content=%include_content%"
After the for loop
这篇关于如何扩展环境变量里面的字符串从文件读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!