Windows 批处理文件列出包含特定文件的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2700512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:20:10  来源:igfitidea点击:

Windows batch file to list folders that have a specific file in them

windowscommand-linecmd

提问by Lee

I'm trying to create a file that has a list of directories that have a specific file name in them.

我正在尝试创建一个文件,其中包含一个包含特定文件名的目录列表。

Let's say I'm trying to find directories that have a file named *.joein them. I initially tried just a simple dir /ad *.joe > dir_list.txt, but it searches the directory names for *.joe, so no go.

假设我正在尝试查找包含命名文件的目录*.joe。我最初只尝试了一个简单的dir /ad *.joe > dir_list.txt,但它会在目录名称中搜索*.joe,所以没有去。

Then I concluded that a for loop was probably my best bet. I started with

然后我得出结论,for 循环可能是我最好的选择。我开始了

for /d /r %a in ('dir *.joe /b') do @echo %a >> dir_list.txt

and it looked like it wasn't executing the dir command. I added the "usebackq", but that seems to only work for the /F command extension.

看起来它没有执行 dir 命令。我添加了“usebackq”,但这似乎只适用于 /F 命令扩展。

Ideas?

想法?

回答by torak

Since "dir /s /b file_name" doesn't cut it, how about the following

既然“ dir /s /b file_name”不剪,那下面呢

for /d /r %a in (*) do  @if exist %a\*.scr (echo %a)

It would appear that inside a batch file the only thing that needs to be esaped is the %a giving

看起来,在批处理文件中,唯一需要避免的是 %a

for /d /r %%a in (*) do  @if exist %%a\*.scr (echo %%a)

回答by Tomalak

Save this to search.bator (something else you can remember)

将此保存到search.bat或(您可以记住的其他内容)

@echo off
setlocal EnableDelayedExpansion
set last=?

if "%1" EQU "" goto usage

for /f %%I in ('dir /s /b /o:n /a-d "%1"') do (
  if !last! NEQ %%~dpI ( 
    set last=%%~dpI
    echo !last!
  )
)
goto end

:usage
echo Please give search parameter.

:end

and use as follows:

并使用如下:

search.bat *.joe >dir_list.txt

Note that it searches within the current path context. You might want to add the .batto a location that is in the PATH, so you can call it from any location.

请注意,它在当前路径上下文中搜索。您可能希望将 添加.bat到 中的位置PATH,以便您可以从任何位置调用它。

回答by Carlos Gutiérrez

This will print the directory and the file name, may or may not be helpful to you:

这将打印目录和文件名,可能对您有帮助,也可能没有帮助:

dir /b /s | findstr "\.joe"

findstruses a regexp.

findstr使用正则表达式。

回答by Tobias Kienzler

dir /s/b *.joe >> dir_list.txtand then a skript in e.g. gawk to get rid of the filenames after the dirnames

dir /s/b *.joe >> dir_list.txt然后在例如 gawk 中使用 skript 去掉目录名之后的文件名