Wenn pest
eine unmittelbare Unterverzeichnis des Stammes ist (dh das aktuelle Verzeichnis, .
), könnten Sie wie folgt vorgehen:
rem // Enumerate immediate child files in the root, output them:
> "temp1.txt" (for %%F in (".\*.f") do @echo %%~fF)
rem // Enumerate immediate subdirectories of the root:
>>"temp1.txt" (
for /D %%D in (".\*.*") do @(
rem // Skip the rest if current subdirectory is the one to exclude:
if /I not "%%~nxD"=="pest" (
rem // Output all files found in the current subdirectory recursively:
pushd "%%~D"
for /R %%E in ("*.f") do @echo %%~E
popd
)
)
)
Das gibt nur Dateien, aber keine Verzeichnisse; wenn Sie so wollen, auch enthalten sein, versuchen Sie den folgenden Code ein:
rem // Output the path to the root directory itself:
> "temp1.txt" (for /D %%D in (".") do @echo %%~fD)
rem // Enumerate immediate child files in the root, output them:
>>"temp1.txt" (for %%F in (".\*.f") do @echo %%~fF)
rem // Enumerate immediate subdirectories of the root:
>>"temp1.txt" (
for /D %%D in (".\*.*") do @(
rem // Skip the rest if current subdirectory is the one to exclude:
if /I not "%%~nxD"=="pest" (
rem // Output the current subdirectory:
echo %%~fD
rem // Output all files found in the current subdirectory recursively:
for /F "eol=| delims=" %%E in ('dir /B /S "%%~D\*.f"') do @echo %%E
)
)
)
Wenn das pest
Unterverzeichnis irgendwo im Baum sein kann, könnte man diesen Ansatz:
@echo off
rem /* Call subroutine with the root directory (the current one), the file pattern
rem and the name of the directory to exclude as arguments: */
> "temp1.txt" call :SUB "." "*.f" "pest"
exit /B
:SUB val_dir_path val_file_pattern val_dir_exclude
rem // Output directory (optionally):
echo %~f1
rem // Enumerate immediate child files and output them:
for %%F in ("%~1\%~2") do echo %%~fF
rem // Enumerate immediate subdirectories:
for /D %%D in ("%~1\*.*") do (
rem // Skip the rest if current subdirectory is the one to exclude:
if /I not "%%~nxD"=="%~3" (
rem /* Recursively call subroutine with the current subdirectory, the file pattern
rem and the name of the directory to exclude as arguments: */
call :SUB "%%~D" "%~2" "%~3"
)
)
Um zu vermeiden, Unterverzeichnisse, die ebenfalls ausgegeben werden, entfernen Sie einfach die Befehlszeile echo %~f1
.
Da dieser Ansatz rekursive Subroutinenaufrufe unterstützt, ist die Leistung merklich schlechter als die Verwendung eines einfachen Befehls dir /S
, falls kein Unterverzeichnis pest
vorhanden ist.
Die Ergebnisse des 'DIR'-Befehls an den' FIND'-Befehl übergeben und die/V-Option mit dem 'FIND'-Befehl verwenden. – Squashman
@Squashman, dies verhindert nicht 'dir/S' von der Aufzählung des Verzeichnisses, was die Absicht der Frage ist, soweit ich es bekommen habe ... – aschipfl