[英]Windows Batch Script to Replace Environment Variables in a File
I want to write a batch file that will take the contents of a file, and replace any environment variable references inside the file with the actual environment variable values. 我想编写一个批处理文件,它将获取文件的内容,并将文件中的任何环境变量引用替换为实际的环境变量值。 Is this possible?
这可能吗? Basically, if a file had this:
基本上,如果文件有这个:
%PROGRAM FILES%\Microsoft SQL Server\
then I would want the file contents to become: 那么我希望文件内容成为:
C:\Program Files\Microsoft SQL Server\
after the batch script ran. 批处理脚本运行后。 This is just one example, but I want ALL environment variables to be expanded.
这只是一个例子,但我希望扩展所有环境变量。 Thanks in advance for any help!
在此先感谢您的帮助!
If powershell is present on the system, you could do: 如果系统上存在powershell,您可以执行以下操作:
powershell -command "get-content 'input.txt' | foreach { [System.Environment]::ExpandEnvironmentVariables($_) } | set-content -path 'output.txt'"
The following works with a plain batch file, though blank lines are removed from the output 以下适用于普通批处理文件,但输出中将删除空白行
@echo off
goto :start
:expand
echo %~1 >> output.txt
goto:eof
:start
echo. > output.txt
for /f "delims=" %%i in (input.txt) do call:expand "%%i"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.