Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于替换文件中的环境变量的Windows批处理脚本_Windows_Scripting_Batch File - Fatal编程技术网

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

用于替换文件中的环境变量的Windows批处理脚本,windows,scripting,batch-file,Windows,Scripting,Batch File,我想编写一个批处理文件,它将获取文件的内容,并用实际的环境变量值替换文件中的任何环境变量引用。这可能吗?基本上,如果文件具有以下内容: %PROGRAM FILES%\Microsoft SQL Server\ 然后我希望文件内容变成: C:\Program Files\Microsoft SQL Server\ 在批处理脚本运行之后。这只是一个例子,但我希望所有的环境变量都被扩展。提前感谢您的帮助 如果系统上存在powershell,您可以执行以下操作: powershell -comma

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

%PROGRAM FILES%\Microsoft SQL Server\
然后我希望文件内容变成:

C:\Program Files\Microsoft SQL Server\

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

如果系统上存在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"