2016-11-15 141 views
0

我目前使用的这条线在Windows批处理文件:获得从目录中的所有文件和所有子目录除了一个子目录

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

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

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

你能提出一个简单的方法吗?

+2

将'DIR'命令的结果传递给'FIND'命令,并使用/ V选项和'FIND'命令。 – Squashman

+1

@Squashman,这并不妨碍'dir/S'列举目录,这是问题的意图,据我所知... – aschipfl

回答

1

如果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命令差。

+0

谢谢。你的第一个版本正是我所需要的。 –

+0

不客气!请考虑通过点击绿色复选标记**接受**答案。阅读以下帮助主题以了解这意味着什么,以及为什么这很重要:[当某人回答我的问题时该怎么办?](http://stackoverflow.com/help/someone-answers)答案是“接受”?](http://stackoverflow.com/help/accepted-answer) – aschipfl

0

你可能使用目录排除在设施ROBOCOPY:

@Echo Off 

(Set SrcDir=C:\Users\Parker\Documents) 
(Set SrcMsk=*.f) 
(Set ToExcl=pest) 
(Set OutPut=temp1.txt) 

>"%OutPut%" (For /F "Tokens=*" %%A In ('RoboCopy "%SrcDir%" NULL %SrcMsk%^ 
/L /S /FP /NDL /NS /NC /NJH /NJS /XD "%ToExcl%"') Do Echo=%%A) 

请根据需要四个parenthesesed线的相关变化。

+0

由于某种原因,这不仅返回* .f文件,而且仅返回所有这些文件? –

+0

道歉,在我的答案中有一个错字_(现在修正了%ScrMsk%而不是%SrcMsk%)_。 – Compo

相关问题