1

I am having an issue with a build batch file from a third-party project I am working with. It does a check for if the OS is Windows by using the ver command. Unfortunately, I cannot seem to print the ver command from the batch file as it does on the command line. The command line output is:

Microsoft Windows [Version 10.0.15063]

However, when I run in a batch file:

set v ver
echo %v%

The output is instead:

v=10.0
VBOX_MSI_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\
VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\
VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\

Which appears to be giving me the windows version (though not in as full detail), and then versions of a bunch of other things. Is there a reason why this would work differently or is failing? Does anyone know how to call the original behavior which is observed at the command line? The third party script appears to be expecting the behavior for the command line version.

Update: Based on the answer below, I have found that while this was an issue, it was not the issue that was actually causing the third-party batch file to fail. That seems like it must be due to an issue with findstr or %ERRORLEVEL% NEQ 0 (since the problem is that it thinks it did not find the string "Windows" in the version).

  • Based on @JoeMuc2008 I am reminded that I did not call the set command properly. However, whatever is in ver is definitely still not right, since the original third party code was unable to find the word "Windows" in it... (and this clearly worked on other machines with different configs). I don't use .bat files much these days, so will try to recall the right way to output the full contents. – Namey Aug 3 '17 at 21:52
  • See my answer below when you get a chance. You can use that method in a batch file to get the expected results and value from that command just like I explained. It seems to return a blank line first and then the Windows Version ~ so piping that command to the find "." command and using that returns only the expected value that you can set as a variable, etc. – Formerly Pimp Juice IT Aug 3 '17 at 22:18
  • 1
    Thanks. Accepted. I never thought I would ever be so pleased with something I got from McDonald's. – Namey Aug 3 '17 at 22:27
1

Pipe the ver command to the find command with the "." value just as shown below to tell it to return only the line with a dot from the ver command since it seems to also return a blank line first. This way piping it over to find "." it will only redirect overwrite the line with a dot to the file.

ver | find ".">temp.txt
SET /P v= < temp.txt
ECHO %v%

Further Resources

| improve this answer | |
  • I stand corrected. Their script has an issue but it is not due to the version. Accepting and withdrawing question. – Namey Aug 3 '17 at 22:22
1

To clarify first, if you use set without a value assignment (like set value=10), then set will return all environment variables that begin like the expression you specified.

set v

will actually list all variables whose name begins with a v.

I assume that you have previously created an environment variable v by accident which contains "10.0". That's what actually causes the first line of your output. I am pretty sure that is not a result of cmd. It is just included in the list because its name also matches the given search pattern.

Unfortunately, this is where my ideas are ending. There are some solutions on how to capture a command's output into an environment variable but I didn't get it to work with ver:

https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-one-line-in-windows-cmd https://stackoverflow.com/questions/2323292/windows-batch-assign-output-of-a-program-to-a-variable

Sorry I cannot really give you the desired solution. Hope I could help to sort a few things out though. Wishing you good luck finding the perfect solution somewhere here.

| improve this answer | |
1

Try:

for /f "delims=" %a in ('ver') do @set version=%a

Use double escapes (% becomes %%) when using within a batch file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.