如果pest
是根立即子目录(即当前目录,.
),你可以做到以下几点:
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
)
)
)
这仅返回文件,但没有目录;如果你想这样被列入过,请尝试以下代码:
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
)
)
)
如果pest
子目录可以在树中的任何地方,你可以使用这种方法:
@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"
)
)
为了避免子目录也要输出,只需删除命令行echo %~f1
即可。
由于此方法具有递归子程序调用,因此在没有pest
子目录的情况下,性能明显比使用简单的dir /S
命令差。
将'DIR'命令的结果传递给'FIND'命令,并使用/ V选项和'FIND'命令。 – Squashman
@Squashman,这并不妨碍'dir/S'列举目录,这是问题的意图,据我所知... – aschipfl