Windows Script package for replacing environment variables in a file

I want to write a batch file that will contain the contents of the file, and replace any references to the environment variables inside the file with the actual values ​​of the environment variables. Is it possible? Basically, if the file had this:

%PROGRAM FILES%\Microsoft SQL Server\

then I would like the contents of the file to become:

C:\Program Files\Microsoft SQL Server\

after running the batch script. This is just one example, but I want ALL environment variables to be extended. Thanks in advance for your help!

+3
source share
1 answer

If powershell is present on the system, you can do:

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

The following works with a simple batch file, although empty 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"
+2
source

Source: https://habr.com/ru/post/1748248/


All Articles