私はあなたのバッチファイルの複数のファイルやディレクトリの引数を処理する方法を表示する(コマンドラインまたはでドラッグ&配信-ドロップ)。提供されたすべてのディレクトリ引数について、その内容が列挙されます。あなたは空でない値に変数RECURSIVE
を設定している場合、ディレクトリを再帰的に処理されます。
@echo off
rem /* Define recursive mode globally: */
set "RECURSIVE=" & rem // (empty means off, so non-recursive)
rem /* Define list file name: */
set "LISTFILE=%~n0.lst" & rem // (use batch script name + `.lst`)
rem // Determine target location, depending on first argument:
set "ATTR=%~a1"
if defined ATTR (
if "%ATTR:~,1%"=="d" (
set "LOCATION=%~f1\%LISTFILE%"
) else (
set "LOCATION=%~dp1%LISTFILE%"
)
)
set /A "COUNTER=0"
rem // Loop through all arguments:
for %%A in (%*) do (
rem // Call sub-routine for each argument:
if not "%%~A"=="" call :RESOLVE "%%~fA"
)
exit /B
:RESOLVE
rem // Check if item is file or directory:
set "ATTR=%~a1"
if not "%ATTR:~,1%"=="d" (
rem // Process a single file:
call :PROCESS "%~1"
) else (
rem // Check for recursive mode:
if not defined RECURSIVE (
rem // Process all files in a directory:
for %%B in ("%~1\*.*") do (
call :PROCESS "%%~B"
)
) else (
rem // Process all files in a directory recursively:
for /R "%~1" %%B in ("*.*") do (
call :PROCESS "%%~B"
)
)
)
exit /B
:PROCESS
rem // Process a single file:
set /A "COUNTER+=1"
if %COUNTER% EQU 1 (
> "%LOCATION%" echo file%COUNTER%=%~nx1
) else (
>> "%LOCATION%" echo file%COUNTER%=%~nx1
)
exit /B
:
@echo off
rem /* Define recursive mode globally: */
set "RECURSIVE="
rem // Loop through all arguments:
for %%A in (%*) do (
rem // Call sub-routine for each argument:
if not "%%~A"=="" call :RESOLVE "%%~fA"
)
pause
exit /B
:RESOLVE
rem // Check if item is file or directory:
set "ATTR=%~a1"
if not "%ATTR:~,1%"=="d" (
rem // Process a single file:
call :PROCESS "%~1"
) else (
rem // Check for recursive mode:
if not defined RECURSIVE (
rem // Process all files in a directory:
for %%B in ("%~1\*.*") do (
call :PROCESS "%%~B"
)
) else (
rem // Process all files in a directory recursively:
for /R "%~1" %%B in ("*.*") do (
call :PROCESS "%%~B"
)
)
)
exit /B
:PROCESS
rem // Process a single file (simply echo it):
echo %~1
exit /B
があなたの仕事のためにこれを適用するには、次のスクリプトを使用することができます
ありがとう! これは私の問題を完全に解決し、あなたのコメントと説明は私の学習と理解に役立ちます。 – Parduz