computing
  • 0

Batch Labels In a For Loop

  • 0

I have been using a scripting solution that was provided to me by nbrane and it’s perfect for what I need. I use it ALL the time because it is awesome!!
It looks like this:

———————-Begin Script———————-

@Echo Off
for /f %%a in (\\Server1\apps$\Scripts\CompList\Computers-ON.txt) do (
echo processing to computer %%a

(Commands for each computer go here)

::——–End
)

———————-End Script———————-

However, now I have another twist. I need to add some decision logic to the script. I’ve found some code that will determine the version of the O/S, and now I need a: GoTo WinXP or GoTo Win7 However, when I add the label :WinXP or :Win7 ,the script will not run—Error: “The syntax of the command is incorrect.”

This is the script that will NOT execute:

———————-Begin Script———————-

@Echo Off
for /f %%a in (\\Server1\apps$\Scripts\CompList\Computers-ON.txt) do (
echo processing to computer %%a

for /f “tokens=2*” %%i IN (‘reg query “HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion” /v ProductName ^| Find “ProductName”‘) DO set OSver=%%i %%j

echo %OSver% | find “Windows XP” > nul
if %ERRORLEVEL% == 0 GoTo Win_XP

echo %OSver% | find “Windows 7″ > nul
if %ERRORLEVEL% == 0 GoTo Win_7

:Win_XP
xcopy /O /Y \\Server1\apps$\Application\Shortcut\*.* \\%%a\c$”\Documents and Settings\All Users\Desktop\*.*”
GoTo NextComp

:Win_7
xcopy /O /Y \\Server1\apps$\Application\Shortcut\*.* \\%%a\c$\Users\Public\Desktop\*.*
GoTo NextComp

:NextComp

)

::——–End

———————-End Script———————-

If I remove all of the labels (:WinXP, :Win7, :NextComp) the script will run—of course I get other errors, but that’s to be expected, since I don’t have the labels in there.

Does anyone have any ideas or is more info needed?

Thanks in advance!!! Any help is much appreciated.

——-Mark

Share

1 Answer

  1. You can avoid this whole mess:

    echo %CurCompOS% | find “Windows XP” > nul
    if %ERRORLEVEL% == 0 GoTo Win_XP

    echo %CurCompOS% | find “Windows 7” > nul
    if %ERRORLEVEL% == 0 GoTo Win_7

    with:

    goto :%CurCompOS%

    =====================================
    Life is too important to be taken seriously.

    M2

    • 0