Re: Limits on Parens in Batch files

From: Matthias Tacke (Matthias_at_Tacke.de)
Date: 12/07/04


Date: Tue, 07 Dec 2004 21:52:16 +0100

HipHopHank schrieb:
> The code I was having a problem with invoved several labels and the goto
> command. All the labels and the goto's were within the parens, but they just
> seemed to make the command processor nuts. The best I could do was get a
> single pass through the for loop and then the program stopped. I don't know
> at this point if it is the 'FOR' command or the Parens that had the problem.
> I solved it by using the call command to call a label within the file. That
> is working fine.
>
> This works:
>
But there are flaws.

In a batch you've to take care yourself that a called routine is seperated
from normal flow.

> @echo off
> setlocal enableextensions enabledelayedexpansion
> d:
> cd \programs\batch
> for /f %%i IN (MachineList.txt) do (
> echo %%i
> call :worker %%i
> )
> endlocal
>
The endlocal is implied when terminating the batch and and resets path
and variables to the state when setlocal was issued. Here is a missing:

goto :eof

> :worker
> nbtstat -a %1 | find "<1E> GROUP"
> if !ERRORLEVEL! NEQ 0 goto F
>
The usage of delayed expansion isn't neccessary.

You could use conditional execution || or && to control flow as an
alternative.

> dir \\%1\Admin$\explorer.exe /b
> if !ERRORLEVEL! NEQ 0 goto X
>
> freedisk /S %1 /D c: 10MB
> if !ERRORLEVEL! NEQ 0 goto N
> echo %1 found>>foundlog.txt
> goto L
> :F
> echo F
> goto L
> :X
> echo X
> goto L
> :N
> echo N
> :L
> Echo L
> goto :EOF

The :eof doesn't need to be defined, it is implied.

> This doesn't:
<snipped>

> The labels are only stubs, something useful will be there soon, now that I
> have the basic logic working. The only thing I don't know at this point is
> if the set local carries over through the call. I don't seem to need it, but
> I don't like to use code I don't understand.
>
This batch should do the same as your sample. Readability is a matter
of personal preferrence - I like it more compact.

@echo off&setlocal EnableExtensions EnableDelayedExpansion
cd /D d:\programs\batch
for /f %%A IN (MachineList.txt) do echo %%A&call :worker %%A
goto :eof
:worker
nbtstat -a %1 |find "<1E> GROUP"||(echo f &exit /b 1)
dir \\%1\Admin$\explorer.exe /b ||(echo X & exit /b 2)
freedisk /S %1 /D c: 10MB ||(echo N & exit /b 3)
echo %1 found>>foundlog.txt

My uasge of exit /b errorlevel allows a sub or batch to pass information
to the caller. (Though not acted upon here)

HTH

-- 
Gruesse  Greetings Saludos   Saluti    Salutations
Matthias
---------+---------+---------+---------+---------+---------+---------+


Relevant Pages

  • Re: Limits on Parens in Batch files
    ... seemed to make the command processor nuts. ... at this point if it is the 'FOR' command or the Parens that had the problem. ... NEQ 0 goto F ... echo %1 found>>foundlog.txt ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Solved: How to wait for the right time to execute FBA Generic Comm
    ... @echo off ... if %errorlevel%==5 echo A command syntax error occurred. ... if not %errorlevel%==4 goto ERROR ... If objItem.Name = "Local Area Connection" Then ...
    (microsoft.public.windowsxp.embedded)
  • Re: script to unmap and remap network drives
    ... goto:eof ... echo net use %1 /d ... Since there is no difference between running a command from ...
    (microsoft.public.windows.server.scripting)
  • Re: 16-bit under WinXP Pro?
    ... @echo off ... goto end ... del epurmess.res ... The echo message command is useful when echo is turned off. ...
    (microsoft.public.windowsxp.help_and_support)
  • a turbo assembly language
    ... if not exist %1.m goto pst1_n_as2 ... echo Uso con la shell: ... int catstr ... õ2\nmov õ1, eax"); ...
    (alt.lang.asm)