1.

Solve : Batch File Question - "Set" Command?

Answer»

Can you use the "set" command inside of another "set" command? In my batch file, I've got a list of file names at the top which are intended to trickle down into the processes below, so that it eliminates the need to type them all twice (as they may change often). However, it keeps pulling only the first one out of nine in the final result.

For the final %mapname% variables, it doesn't seem to bring the user's choice into PLAY. It just uses "level1.map".

BTW...yes I am a noob with writing batch files, so if something doesn't look quite right, that's why.

Any thoughts?

Here's my code:
Code: [Select]:Duke Nukem 3D Batch File
:Written by Dez

@echo off

:Below are the map choices. Please change the values to
:coorespond with the actual map names.

set map1=Level1.map
set map2=Level2.map
set map3=Level3.map
set map4=Level4.map
set map5=Level5.map
set map6=Level6.map
set map7=Level7.map
set map8=Level8.map
set map9=Level9.map

cls
ECHO.Duke Nukem 3D Multiplayer Game Launcher
ECHO.Batch File Coded by DeZ
ECHO.


ECHO.
ECHO.
ECHO.Please select a map from the following:
ECHO.
ECHO. 1. %map1%
ECHO. 2. %map2%
ECHO. 3. %map3%
ECHO. 4. %map4%
ECHO. 5. %map5%
ECHO. 6. %map6%
ECHO. 7. %map7%
ECHO. 8. %map8%
ECHO. 9. %map9%

ECHO.
choice /c:123456789 /n Enter the number of your choice:
ECHO.
if errorlevel ==9 set mapname=%map9%
if errorlevel ==8 set mapname=%map8%
if errorlevel ==7 set mapname=%map7%
if errorlevel ==6 set mapname=%map6%
if errorlevel ==5 set mapname=%map5%
if errorlevel ==4 set mapname=%map4%
if errorlevel ==3 set mapname=%map3%
if errorlevel ==2 set mapname=%map2%
if errorlevel ==1 set mapname=%map1%

choice /n Are you the host (Y,N)?
ECHO.
if errorlevel ==2 goto client
if errorlevel ==1 goto host


:client
set /p ip=Host's IP Address?
ECHO.

@ECHO ON
duke3d.exe -map %mapname% /net /n0 %ip%
@ECHO OFF

goto end


:host
@ECHO ON
duke3d.exe -map %mapname% /net /n0
@ECHO OFF

:end
I think you have got the errorlevel checking a bit wrong.

It's not

if errorlevel ==1 (do something)

You don't need the double equals signs. "Errorlevel" is not a variable, and if it were it would have percent signs before and after.

It's

if errorlevel 1 (do something)

See here

http://www.google.co.uk/search?source=ig&hl=en&q=if+errorlevel+1&meta=&aq=t&oq=if+error



Contrex: Understood. I have now removed the "==" from the errorlevel checking, and my script still works...but it still doesn't report back the correct user choice. It's still pulling "level1.map" for some reason.

Thanks just the same though...

*Edit* - I just thought I'd add that I'm really WONDERING if the set command can be used the way I have. It basically has to lookup two values. For example, if the user chooses map 3, then when it sends the final command, it first has to lookup %mapname% to see that it's referencing %map3%, and then back to %map3% which is referencing level3.map. I'm just wondering if it even allows multiple uses like that.When you are checking the errorlevel, you have to jump out when you hit the right one otherwise the batch will just go down to the lowest one and that will be the result.

Code: [Select]:Duke Nukem 3D Batch File
:Written by Dez

@echo off

:Below are the map choices. Please change the values to
:coorespond with the actual map names.

set map1=Level1.map
set map2=Level2.map
set map3=Level3.map
set map4=Level4.map
set map5=Level5.map
set map6=Level6.map
set map7=Level7.map
set map8=Level8.map
set map9=Level9.map

cls
ECHO.Duke Nukem 3D Multiplayer Game Launcher
ECHO.Batch File Coded by DeZ
ECHO.


ECHO.
ECHO.
ECHO.Please select a map from the following:
ECHO.
ECHO. 1. %map1%
ECHO. 2. %map2%
ECHO. 3. %map3%
ECHO. 4. %map4%
ECHO. 5. %map5%
ECHO. 6. %map6%
ECHO. 7. %map7%
ECHO. 8. %map8%
ECHO. 9. %map9%

ECHO.
choice /c:123456789 /n Enter the number of your choice:
ECHO.
if errorlevel 9 goto map9
if errorlevel 8 goto map8
if errorlevel 7 goto map7
if errorlevel 6 goto map6
if errorlevel 5 goto map5
if errorlevel 4 goto map4
if errorlevel 3 goto map3
if errorlevel 2 goto map2
if errorlevel 1 goto map1


:map9
set mapname=%map9%
goto next

:map8
set mapname=%map8%
goto next

:map7
set mapname=%map7%
goto next

:map6
set mapname=%map6%
goto next

:map5
set mapname=%map5%
goto next

:map4
set mapname=%map4%
goto next

:map3
set mapname=%map3%
goto next

:map2
set mapname=%map2%
goto next

:map1
set mapname=%map1%
goto next

:next

choice /n Are you the host (Y,N)?
ECHO.
if errorlevel 2 goto client
if errorlevel 1 goto host


:client
set /p ip=Host's IP Address?
ECHO.

@ECHO ON
duke3d.exe -map %mapname% /net /n0 %ip%
@ECHO OFF

goto end


:host
@ECHO ON
duke3d.exe -map %mapname% /net /n0
@ECHO OFF

:end

Contrex: That worked just fine. Thanks for your help! I think I was just trying to be a little bit lazy and combine some steps. I was just under the assumption that once it found the MATCHING user choice, it would stop there.

Thanks again!did you see I edited my post? Seems to work OK
I do now, and in fact your code now looks virtually identical to mine.

Thanks again.



Discussion

No Comment Found