Re: Limits on Parens in Batch files
From: Matthias Tacke (Matthias_at_Tacke.de)
Date: 12/07/04
- Next message: L. Athena W.: "Re: Help: Unicode: UTF-8 CMD prompt"
- Previous message: erez: "mountvol"
- In reply to: HipHopHank: "Re: Limits on Parens in Batch files"
- Messages sorted by: [ date ] [ thread ]
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 ---------+---------+---------+---------+---------+---------+---------+
- Next message: L. Athena W.: "Re: Help: Unicode: UTF-8 CMD prompt"
- Previous message: erez: "mountvol"
- In reply to: HipHopHank: "Re: Limits on Parens in Batch files"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|