如何扩展环境变量里面的字符串从文件读取? [英] How to expand environment variable inside string read from file?

查看:163
本文介绍了如何扩展环境变量里面的字符串从文件读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含路径列表的文件。其中一些可能包括环境变量,例如%USERPROFILE%



示例:

 %USERPROFILE%\dir 
C:\another\dir

然后我使用这段代码读取所有的行并连接到; (可以使用cat和tr命令因为git):

  set include_file = C:\path\to\file.txt 
/ fdelims =%% i in('cat%include_file%^ | tr\\\\
;')do set include_content = %% i
/ pre>

这将创建一个变量,其中包含字符串%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 running echo %USERPROFILE% would give you something like C:\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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆