Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problems with variables when trying to log output

I’m currently working on a batch file, which runs several commands, reads values from text files, etc.
The problem I’m facing is that I cannot set variables properly.

The following code snippet demonstrates my problem:

>> %userprofile%\AppData\Local\Temp\test.log 2>&1 (
    echo ### Fetching language...
    powershell -c "(Get-UICulture).Parent.Name" > lang.txt
    set /p LANG=<lang.txt
    echo Your language = %LANG%
)

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

### Fetching language...
Your language =

If remove the line >> %userprofile%\AppData\Local\Temp\test.log 2>&1 everything works fine, but the output is not written to a file.

What do I miss here?

Thank you so far!

EDIT
The question has been ask already here: Variables are not behaving as expected

Thank you all anyway!

>Solution :

Your %LANG%-Var is evaluated when the interpreter sees the opening bracket in your first line. This can be disabled this way:

SETLOCAL ENABLEDELAYEDEXPANSION
>> %userprofile%\AppData\Local\Temp\test.log 2>&1 (
  echo ### Fetching language...
  powershell -c "(Get-UICulture).Parent.Name" > lang.txt
  set /p LANG=<lang.txt
  echo Your language = !LANG!
  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading