从目录和所有子目录获取所有文件,但不包含一个子目录

帕克·刘易斯

我目前在Windows批处理文件中使用以下行:

@ REM List all *.f in current dir and all its subdirs
dir *.f /B /S > temp1.txt

不幸的是,其中一个子目录(将其命名为pest)具有非常大的子树,这使处理过程非常缓慢。由于pest子目录对于此特定任务实际上并不重要(不应包含任何相关文件),因此我想将其从搜索中排除。

因此,我不想在当前目录和所有子目录中搜索,而是想在当前目录和除之外的所有子目录中搜索pest

您能提出一种简单的方法吗?

阿希普

如果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

由于此方法具有递归子例程调用的功能,因此dir /S在没有pest子目录的情况下,性能明显比使用简单命令

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从所有子目录删除文件,保留一个子目录以外的文件夹结构

来自分类Dev

搜索包含 0 个文件的所有子目录

来自分类Dev

忽略除一个文件外的所有文件和子目录中的文件

来自分类Dev

忽略除一个文件外的所有文件和子目录中的文件

来自分类Dev

301目录URL及其所有内容到另一个子目录(子目录除外)

来自分类Dev

不包含任何文件的所有子目录的路径

来自分类Dev

ZSH 和 globstar 匹配子目录中的所有文件,但不匹配目录

来自分类Dev

Git:忽略所有文件和目录,但不忽略子目录中的某些代码

来自分类Dev

Nginx将子目录的所有内容重定向到另一个子目录

来自分类Dev

sftp:上传文件夹中包含的所有文件,目录和子目录

来自分类Dev

将子目录中的所有.smi文件打开到一个文件中

来自分类Dev

输入多个目录,输入子目录,然后使用bash将所有文件移至一个目录

来自分类Dev

所有文件和子目录的循环 cat 命令

来自分类Dev

递归删除除几个子目录外的所有子目录

来自分类Dev

什么是性能更好的-一个目录中有许多文件,或者每个子目录中有多个子目录?

来自分类Dev

Linux:删除只有一个文件且没有子目录的所有目录

来自分类Dev

无法从C:\目录和子目录获取所有文件

来自分类Dev

PHP:使用 scandir 从多个子目录中获取所有 zip 文件

来自分类Dev

Apache重定向除一个目录以外的所有子目录

来自分类Dev

从目录复制所有文件,而不创建子目录

来自分类Dev

查找目录及其子目录中的所有文件

来自分类Dev

解压目录和子目录中的所有.gz

来自分类Dev

递归打印所有目录和子目录

来自分类Dev

打印所有目录和子目录功能

来自分类Dev

rsync 排除目录和所有子目录

来自分类Dev

将所有具有相同名称的文件从子目录移动到一个目录中

来自分类Dev

Visual Studio 2015和Gulp:获取所有子目录中的所有文件

来自分类Dev

为特定目录中除子目录外的所有C ++文件生成一个标记文件

来自分类Dev

PHP获取所有文件,但不在子目录中

Related 相关文章

  1. 1

    从所有子目录删除文件,保留一个子目录以外的文件夹结构

  2. 2

    搜索包含 0 个文件的所有子目录

  3. 3

    忽略除一个文件外的所有文件和子目录中的文件

  4. 4

    忽略除一个文件外的所有文件和子目录中的文件

  5. 5

    301目录URL及其所有内容到另一个子目录(子目录除外)

  6. 6

    不包含任何文件的所有子目录的路径

  7. 7

    ZSH 和 globstar 匹配子目录中的所有文件,但不匹配目录

  8. 8

    Git:忽略所有文件和目录,但不忽略子目录中的某些代码

  9. 9

    Nginx将子目录的所有内容重定向到另一个子目录

  10. 10

    sftp:上传文件夹中包含的所有文件,目录和子目录

  11. 11

    将子目录中的所有.smi文件打开到一个文件中

  12. 12

    输入多个目录,输入子目录,然后使用bash将所有文件移至一个目录

  13. 13

    所有文件和子目录的循环 cat 命令

  14. 14

    递归删除除几个子目录外的所有子目录

  15. 15

    什么是性能更好的-一个目录中有许多文件,或者每个子目录中有多个子目录?

  16. 16

    Linux:删除只有一个文件且没有子目录的所有目录

  17. 17

    无法从C:\目录和子目录获取所有文件

  18. 18

    PHP:使用 scandir 从多个子目录中获取所有 zip 文件

  19. 19

    Apache重定向除一个目录以外的所有子目录

  20. 20

    从目录复制所有文件,而不创建子目录

  21. 21

    查找目录及其子目录中的所有文件

  22. 22

    解压目录和子目录中的所有.gz

  23. 23

    递归打印所有目录和子目录

  24. 24

    打印所有目录和子目录功能

  25. 25

    rsync 排除目录和所有子目录

  26. 26

    将所有具有相同名称的文件从子目录移动到一个目录中

  27. 27

    Visual Studio 2015和Gulp:获取所有子目录中的所有文件

  28. 28

    为特定目录中除子目录外的所有C ++文件生成一个标记文件

  29. 29

    PHP获取所有文件,但不在子目录中

热门标签

归档