Windows batch file for a list of folders that have a specific file

I am trying to create a file with a list of directories that have a specific file name.

Let's say I'm trying to find directories that have a file named *.joe . At first I tried just dir /ad *.joe > dir_list.txt , but it looks for the directory names for *.joe , so there is no need to go.

Then I came to the conclusion that the for loop was probably the best choice. I started with

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

and it looks like he did not execute the dir command. I added "usebackq", but it seems to work only for extending the / F command.

Ideas?

+4
source share
4 answers

Since " dir /s /b file_name " does not cut it, how about the following

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

It seems like the only thing you need to use inside the batch file is% a grant

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

Store this value in search.bat or (something else that you can remember)

 @echo off setlocal EnableDelayedExpansion set last=? if "%1" EQU "" goto usage for /f %%I in ('dir /s /b /o:n /ad "%1"') do ( if !last! NEQ %%~dpI ( set last=%%~dpI echo !last! ) ) goto end :usage echo Please give search parameter. :end 

and use the following:

 search.bat *.joe >dir_list.txt 

Notice what it is looking for in the current context of the path. You might want to add .bat to a location located in PATH so you can call it from anywhere.

+1
source

This will print the directory and file name, whether or not it may be useful for you:

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

findstr uses a regex.

+1
source

dir /s/b *.joe >> dir_list.txt , and then skript, for example. gawk to get rid of file names after dirnames

0
source

Source: https://habr.com/ru/post/1307799/


All Articles