command retunrs no value even though entry exists

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Yanta
Posts: 48
Joined: 01 Sep 2019 07:08

Re: command returns no value even though entry exists

#16 Post by Yanta » 02 Oct 2020 16:15

Code: Select all

Rem     Max Payne 2
        Set GamePath=
        Set D2Test=
        for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Max payne 2" set GamePath=%%b
	Echo %time% Path: "%GamePath%" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        IF NOT "%GamePath%"=="" IF EXIST "%GamePath%\" IF EXIST "%SrcPath%\MaxPayne2\" IF EXIST "%RegPath%\MaxPayn2.Reg" (
            Echo %time% Checking for Max Payne 2 Save games  >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
            REG IMPORT "%RegPath%\MaxPayne2.Reg" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
            FOR /f "tokens=5*" %%a in ('REG QUERY "HKEY_CURRENT_USER\Software\Remedy Entertainment\Max Payne 2\Save Game" /v "Last Saved Game Filename"') do set D2Test=%%~dpb
            set D2Test=%D2Test:\\=\%
            IF NOT "%D2Test%"=="" IF NOT EXIST "%D2Test%\" xcopy "%SrcPath%\MaxPayne2" "%D2Test%" /e/q/y/c/k/i/h/x >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        )
If Max Payne 2 is installed I want to find out where the save games are stored then restore the saved games to the correct location. The problem I can see with the above code is that %D2Test%\ will probably exist I only restore the files if it doesn't. That's flawed right there.

So, Max Payne 2 now looks like...

Code: Select all

Rem     Max Payne 2
        Set GamePath=
        Set D2Test=
        for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Max payne 2" set GamePath=%%b
	Echo %time% Path: "%GamePath%" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        IF NOT "%GamePath%"=="" IF EXIST "%GamePath%\" IF EXIST "%RegPath%\MaxPayn2.Reg" REG IMPORT "%RegPath%\MaxPayne2.Reg" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        FOR /f "tokens=5*" %%a in ('REG QUERY "HKEY_CURRENT_USER\Software\Remedy Entertainment\Max Payne 2\Save Game" /v "Last Saved Game Filename"') do set D2Test=%%~dpb
	IF NOT "%D2Test%"=="" (
		set D2Test=%D2Test:\\=\%
		xcopy "%SrcPath%\MaxPayne2" "%D2Test%" /e/q/y/c/k/i/h/x >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
	)
But I don't understand variable expansion issue.

I clearly have a massive gap in my knowledge as this delayedexpansion stuff just doesn't gel.

For example, after coding 184 games, not a single one of these works within the script, and I've removed the surrounding IF /I "%DoGame:~0,1%"=="Y" (...). The backup script which is now identical (except the reg commands are export vs import), does work, but these don't. They ALL return null values. This doesn't work (restore/import)...

Code: Select all

Rem     Mass Effect.
        Set GamePath=
        Echo %time% Checking for Mass Effect >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Mass Effect" set GamePath=%%b
		Echo %time% Path: "%Gamepath%" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        IF EXIST "%GamePath%\" IF NOT "%GamePath%"=="" (
	    Echo %time% Restore Mass Effect >>C:\%USERDOMAIN%.Backup.Log 2>&1        
            IF NOT EXIST "%RegPath%\MassEffect.Reg" IF EXIST "%RegPath%\MassEffect11.Reg" REG IMPORT "%RegPath%\MassEffect11.Reg" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
            IF NOT EXIST "%RegPath%\MassEffect.Reg" IF EXIST "%RegPath%\MassEffect12.Reg" REG IMPORT "%RegPath%\MassEffect12.Reg" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
            IF NOT EXIST "%RegPath%\MassEffect.Reg" IF EXIST "%RegPath%\MassEffect13.Reg" REG IMPORT "%RegPath%\MassEffect13.Reg" >>C:\%USERDOMAIN%.PostInstall.Log 2>&1
        )
But this does (backup/export)...

Code: Select all

Rem Mass Effect
	Set GamePath=
	Echo %time% Checking for Mass Effect >>C:\%USERDOMAIN%.Backup.Log 2>&1
	for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Mass Effect" set GamePath=%%b
	Echo %time% Path: "%GamePath%" >>C:\%USERDOMAIN%.Backup.Log 2>&1
	IF EXIST "%GamePath%\" IF NOT "%GamePath%"=="" (
		Echo %time% Backup Mass Effect >>C:\%USERDOMAIN%.Backup.Log 2>&1
		REG EXPORT "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\BioWare\Mass Effect" "%RegPath%\MassEffect11.reg" /y >>C:\%USERDOMAIN%.Backup.Log 2>&1
		REG EXPORT "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Electronic Arts\Electronic Arts\Mass Effect (tm)" "%RegPath%\MassEffect12.reg" /y >>C:\%USERDOMAIN%.Backup.Log 2>&1
		REG EXPORT "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1B0FBB9A-995D-47cd-87CD-13E68B676E4F}" "%RegPath%\MassEffect13.reg" /y >>C:\%USERDOMAIN%.Backup.Log 2>&1
	)
And so on for all 184 games.

I figured the folders.txt file is ok given that the back up works, and the for /f command works from a command line, but I manually typed the entire file from scratch anyway.

...while all that do (for example use GamePath) shouldn't work correct

All games are preceded with Set GamePath=

But in trying to understand variable expansion, is my take away, "Never create a variable within an IF ( ... ) block"?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: command returns no value even though entry exists

#17 Post by penpen » 05 Oct 2020 06:02

Yanta wrote:
02 Oct 2020 16:15
But I don't understand variable expansion issue.
Normal variable expansion is performed (once) after a (atomic/complex) statement is read into memory.
Delayed expansion is performed right (once) before an atomic statement is executed.
Therefore you can't change the value of a normal expanded environment variable within a block (= compound statement):
It gets replaced before you change the variable content.

You might look into the example i gave above.

For more detailed information you could see:
https://stackoverflow.com/questions/409 ... 33#4095133

Yanta wrote:
02 Oct 2020 16:15
The backup script which is now identical (except the reg commands are export vs import), does work, but these don't. They ALL return null values. This doesn't work (restore/import)...
I can't see your folders and which variables your script are defined. If i had to guess i would first check variables in the "if"-commands (which seems to be the main difference between those two (working versus non working) sources.

So you should debug your program. For example you could could:
- activate echoing (or not disable it at start) (remove "@echo off" or add an "echo on" right before the lines you want to debug,
- add echo statements like 'echo(RegPath="%RegPath%"', or
- echo a command instead of executing it (by prepending "echo(").

If nothing else seems to help, you might consider the following:
You should edit the code for one game at a time and program it bottom up. So first don't use any variables and hardcode any directory and make it work. Then replace one directory at a time with a variable expansion and check if it still works


penpen

Post Reply