用于替换文件中的环境变量的Windows批处理脚本

时间:2010-06-02 21:48:59

标签: windows scripting batch-file

我想编写一个批处理文件,它将获取文件的内容,并将文件中的所有环境变量引用替换为实际的环境变量值。这可能吗?基本上,如果文件有这个:

%PROGRAM FILES%\Microsoft SQL Server\

然后我希望文件内容成为:

C:\Program Files\Microsoft SQL Server\

批处理脚本运行后。这只是一个例子,但我希望扩展所有环境变量。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

如果系统中存在powershell,您可以执行以下操作:

powershell -command "get-content 'input.txt' | foreach { [System.Environment]::ExpandEnvironmentVariables($_) } | set-content -path 'output.txt'"

以下适用于普通批处理文件,但从输出中删除了空白行

@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"
相关问题