Reputation: 21
For some reason I can't get %NetVerClientOrFull% to print inside of the IF statement. This is really weird because it sometimes does and sometimes doesn't even when I don't change anything. I have tested on multiple machines with the same results. If I use %NetVer% inside the IF statement is always prints. Any ideas what is going on?
ECHO Client or Full? %NetVerClientOrFull% (should display Client or Full and sometimes does)
@echo off
set frameworkDirectory=Reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP"
FOR /F "TOKENS=6 DELIMS=\." %%A IN ('%frameworkDirectory%') DO set NetVer=%%A
ECHO The most current version of the .NET Framework installed is: %NetVer%
if "%NetVer%"=="v4" (
set clientOrFullDirectory=Reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP\v4"
FOR /F "TOKENS=7 DELIMS=\." %%A IN ('%clientOrFullDirectory%') DO set NetVerClientOrFull=%%A
ECHO Client or Full? %NetVerClientOrFull%
)
pause
To add onto this it seems like it works on my third run, makes no sense:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Nick>C:\Users\Nick\Desktop\FrameworkVersionChecker.bat
The most current version of the .NET Framework installed is: v4
Client or Full?
Press any key to continue . . .
C:\Users\Nick>C:\Users\Nick\Desktop\FrameworkVersionChecker.bat
The most current version of the .NET Framework installed is: v4
Client or Full?
Press any key to continue . . .
C:\Users\Nick>C:\Users\Nick\Desktop\FrameworkVersionChecker.bat
The most current version of the .NET Framework installed is: v4
Client or Full? Full
Press any key to continue . . .
Upvotes: 2
Views: 6289
Reputation: 3685
That's because parser expands variables when it reads a block, not when it's statements are executed. Your statements inside if ()
block see variables as they were set before entry to this block. This problem, and how to overcome it (by use of delayed expansion) is well explained in set
help. Just execute help set
from command line - there is even similar example included. It's also explained here: windows batch SET inside IF not working
Why it prints on third (and subsequent) run? You do not use setlocal
- thus when you execute your batch next time it will see variables as set in previous run. Remove or remark @echo off
, and execute your batch from freshly started console. You will see how variables get expanded and why it works only 3rd time onwards.
Upvotes: 4