views:

179

answers:

4

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. I initially tried just a simple dir /ad *.joe > dir_list.txt , but it searches the directory names for *.joe, so no go.

Then I concluded that a for loop was probably my best bet. 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. I added the "usebackq", but that seems to only work for the /F command extension.

Ideas?

A: 

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

Tobias Kienzler
+2  A: 

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

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

for /d /r %%a in (*) do  @if exist %%a\*.scr (echo %%a)
torak
Not *exactly* what he's looking for, but +1 because it's by far the easiest solution to the problem. A simple REPLACE("file_name", "") afterwards would give you the desired output - any way to do that in a BAT file?
rwmnau
I was using that before I realized I needed just directory names and not file names; that's when I began experimenting with the dir /ad and I just spiraled downhill from there...
Lee
@Lee: Thanks for shaing your downhill spriral with me ;) Hopefully my edit above brings it to an end.
torak
Whoa. Okay, it works from a command prompt, but not from within a batch file. ??? I didn't specify that in my original question (sorry about that) - I get a "(echo was unexpected at this time." error. Very elegant, though - kudos to you, especially if it can work in a batch file.
Lee
@Lee: Are we there yet?
torak
I think we're there. This is most excellent! Thank you!
Lee
Oh, and for anybody else reading this - if you add a path before the %%a part, you can specify at what point the script begins to search recursively. Very cool.
Lee
A: 

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

@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.

Tomalak
This works - but I don't have a clue what it's doing.
Lee
@Lee: It's a `for` loop over every line the `dir` command returns, this part should be clear. Then it uses the `last` variable to compare every line with the one before. When they differ, it prints the line. This way only the unique lines are printed. Read `for /?` to find out what `%~dpI` means. Search StackOverflow or Google for what `EnableDelayedExpansion` means. ;)
Tomalak
+1  A: 

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.

Carlos Gutiérrez