简体   繁体   English

Windows批处理文件,用于列出其中包含特定文件的文件夹

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

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 *.joe in them. 假设我正在尝试查找其中包含名为*.joe的文件的目录。 I initially tried just a simple dir /ad *.joe > dir_list.txt , but it searches the directory names for *.joe , so no go. 我最初只尝试了一个简单的dir /ad *.joe > dir_list.txt ,但它在目录名中搜索了*.joe ,所以没有去。

Then I concluded that a for loop was probably my best bet. 然后我得出结论,for循环可能是我最好的选择。 I started with 我开始了

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. 它看起来好像没有执行dir命令。 I added the "usebackq", but that seems to only work for the /F command extension. 我添加了“usebackq”,但这似乎只适用于/ F命令扩展。

Ideas? 想法?

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 give

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

Save this to search.bat or (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 .bat to a location that is in the PATH , so you can call it from any location. 您可能希望将.bat添加到PATH的位置,以便可以从任何位置调用它。

This will print the directory and the file name, may or may not be helpful to you: 这将打印目录和文件名,可能对您有帮助,也可能没有帮助:

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

findstr uses a regexp. findstr使用正则表达式。

dir /s/b *.joe >> dir_list.txt然后在例如gawk的skript中删除dirnames之后的文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM