Co można zrobić w batchu? (rekurencyjna obsługa list)

0

Bardzo mi brakowało obsługi plików tekstowych jako list tekstowych w skryptach DOS/Windows, więc w końcu zabrałem się za to i napisałem to sam. Dzięki temu powstał skrypt, który dodaje funkcjonalność obsługi list w dowolnym programie lub skrypcie konsolowym.

Liczba jednocześnie użytych wywołań list jest nieograniczona (można użyć nawet więcej niż 9 list równocześnie), można też używać tę samą listę wielokrotnie w tym samym poleceniu. Polecenie zostanie wywołane dla każdej wartości z każdego użytego pliku listy.

Działa to na tyle dobrze, że na pewno nie tylko mi się przyda. A sam kod skryptu okazał się tak zwarty i szybki, że chyba warto go pokazać.
Aby z niego skorzystać należałoby poniższy tekst zapisać do pliku tekstowego (w trybie CR/LF) LIST.BAT (lub dowolnego innego dla własnych celów).
Skopiowanie z przeglądarki nie powinno zawinąć wierszy.

@echo off
if [%1]==[/?] goto :help
if [%1]==[/l] goto :license
if [%1]==[/L] goto :license

if cmdextversion 2 goto start
verify next command >nul
setlocal enableExtensions
if cmdextversion 2 goto start
echo Sorry, Windows XP or later required.
pause
exit /b 1

:help
set par=[command] [[argument][...]] OR /? OR /L
set com=%~n0 %par%
echo List 1.01
echo.
echo Runs the command with arguments obtained from a specified
echo text files whose filepath preceded by and ending with "@"
echo Possible stream redirect for output.
echo required: Windows XP or later
echo.
echo Syntax: %com%
echo.
echo   command     command to execute
echo   argument    Syntax: {string OR string_head@string_list@string_tail}
echo               argument must be surrounded by quotes if string_list
echo               filepath contains spaces
echo               if string_tail not exist then second @ is not required
echo.
echo               string_list    filepath for regular text file containing
echo                              rows substituted as arguments to the call
echo               string         text string without spaces or tabs
echo               string_head    text string without spaces or tabs
echo               string_tail    text string without spaces or tabs
echo   option ?    Show this help
echo   option L    Show License
echo.
pause
echo Example 1:
echo.
echo parent.txt:
echo this father
echo that mother
echo.
echo children.txt:
echo girl
echo boy
echo.
echo %~n0 echo These @children.txt@s are children of @parent.txt
echo.
echo Output:
echo.
echo These girls are children of this father
echo These girls are children of that mother
echo These boys are children of this father
echo These boys are children of that mother
echo.
pause
echo Example 2:
echo.
echo command.txt:
echo echo
echo type
echo.
echo family members.lst:
echo parent
echo children
echo.
echo %~n0 @command.txt "@family [email protected]"
echo.
echo Output:
echo.
echo parent.txt
echo children.txt
echo this father
echo that mother
echo girl
echo boy
echo.
goto :eof

:license
echo ************************************************************
echo Copyright 2011 by Olamagato ([email protected])
echo.
echo Licensed under the Apache License, Version 2.0 (the "License");
echo you may not use this file except in compliance with the License.
echo You may obtain a copy of the License at
echo.
echo     http://www.apache.org/licenses/LICENSE-2.0
echo.
echo Unless required by applicable law or agreed to in writing,
echo software distributed under the License is distributed on an
echo "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
echo either express or implied. See the License for the specific
echo language governing permissions and limitations under the License.
echo ************************************************************
goto :eof

:start
set parsed=
:nextArg
set checking=%~1
if [] equ [%checking%] goto :listNotFound
for /f "tokens=1,2* delims=@" %%A in ("%checking%") do set argHead=%%A&set argList=%%B&set argTail=%%C
if [@] equ [%checking:~0,1%] (set argTail=%argList%%argTail%&set argList=%argHead%&set argHead=)
if ["%argHead%"] neq ["%checking%"] goto :listFound
set argHead=
set argTail=
set parsed=%parsed% %checking%
shift /1
goto :nextArg

:listFound
for /f "usebackq tokens=*" %%L in ("%argList%") do call %0 %parsed% %argHead%%%L%argTail% %2 %3 %4 %5 %6 %7 %8 %9
goto :end

:listNotFound
call %parsed%

:end
set checking=
set parsed=
0

Nie wgłębiałem się, ale może być potencjalnie ciekawe. Może byś napisał kilka zdań opisujących działanie i wrzucił to jako artykuł?

1 użytkowników online, w tym zalogowanych: 0, gości: 1