# Windows file search question

**URL:** <https://boards.straightdope.com/t/windows-file-search-question/488581>\
**Category:** Factual Questions\
**Created:** [March 6, 2009, 9:24pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581 "2009-03-06T21:24:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [March 6, 2009, 9:24pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/1 "2009-03-06T21:24:17Z")

</div>

I have a conundrum here at work. I have a series of subfolders that are named by year and month. i.e. 200602; 200603 … 200902 where we put files that related to that particular year month combo.

I’m looking for a way to find a file, but I only want to look in the folders 200801 to 200902. I know part of the file name so I can pattern matching. Is there any way to limit a search to some directories in hierarchy but not all of them?

From my perspective I only have two choices, searching once for each directory I want to look in, or search ALL of the directories of the same hierarchy with the ‘search subfolders’ option turned on.

I’m not against using command line, but I don’t see any switches in the dir command that allows me to do something like dir 2008\*\*fileA.txt

This is obviously a windows based machine.

---

<div class="post-metadata">

**Author:** ![Quartz](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/quartz/32/267_2.png) [@Quartz](https://boards.straightdope.com/u/Quartz)\
**Post date:** [March 6, 2009, 9:41pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/2 "2009-03-06T21:41:39Z")

</div>

Try something along the lines of

```auto

For %f in (6 7 8 9) do for %g in (01 02 03 04 05 06 07 08 09 11 12) do dir 200%f%g\*fileA.txt

```

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [March 7, 2009, 4:01am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/3 "2009-03-07T04:01:27Z")

</div>

**Quartz** ’s idea will do everything from 200601 to 200912. That’s not quite what you asked for, but is certainly on the right track.

There’s not a super-easy way to do just the ones 200801 to 200902. But that’s only 13 entries, so we can stand to just enumerate them in code.

```auto

c:>for %d in (200801 200802 200803 200804 200805 200806 200807 200808 200809 200810 200811 200812 200901 200902) do dir %d\{file name pattern goes here}

```

will do your job for you.

If you needed, say, 30 contiguous months out of a period of 10 years, we could add some more algorithmic stuff, but for 13, that’s be worse than this is.

---

<div class="post-metadata">

**Author:** ![Tim\_T-Bonham.net](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@Tim\_T-Bonham.net](https://boards.straightdope.com/u/Tim_T-Bonham.net)\
**Post date:** [March 7, 2009, 9:13am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/4 "2009-03-07T09:13:57Z")

</div>

Create a new temp folder, move those 13 subfolders into it, and then search it. When you’re done, move those folders back from that temp folder to their original spots.

---

<div class="post-metadata">

**Author:** ![Harmonious\_Discord](https://avatars.discourse-cdn.com/v4/letter/h/74df32/32.png) [@Harmonious\_Discord](https://boards.straightdope.com/u/Harmonious_Discord)\
**Post date:** [March 7, 2009, 11:02am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/5 "2009-03-07T11:02:40Z")

</div>

> [@t-bonham@scc.net](#):
>
> Create a new temp folder, move those 13 subfolders into it, and then search it. When you’re done, move those folders back from that temp folder to their original spots.

That is exactly what I was going to say.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [March 7, 2009, 1:00pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/6 "2009-03-07T13:00:06Z")

</div>

How about

for /f “usebackq” %d in (`dir /b 2008*`) do dir %d\*fileA.txt

Note the backquotes inside the parentheses. That makes Windows run the command and use each line of the output in the for loop.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [March 7, 2009, 1:07pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/7 "2009-03-07T13:07:01Z")

</div>

Actually, I think you can use regular single quotes, so it’s just:

for /f %d in (‘dir /b 2008\*’) do dir %d\*fileA.txt

The usebackq thing is only if you want to use single quotes for strings.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [March 7, 2009, 11:48pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/8 "2009-03-07T23:48:31Z")

</div>

> [@Ximenean](#):
>
> Actually, I think you can use regular single quotes, so it’s just:
> 
> for /f %d in (‘dir /b 2008\*’) do dir %d\*fileA.txt
> 
> The usebackq thing is only if you want to use single quotes for strings.

But the OP wanted all 2008s, and two 2009s.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [March 8, 2009, 12:02am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/9 "2009-03-08T00:02:58Z")

</div>

The OP said

> [@](#):
>
> I’m not against using command line, but I don’t see any switches in the dir command that allows me to do something like dir 2008\*\*fileA.txt

That’s exactly what mine and other suggestions do. Obviously you can change the 8 to a 9.  
I got the impression that the OP limited it to 200902 simply because there is no 200903 directory yet.

---

<div class="post-metadata">

**Author:** ![Keeve](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@Keeve](https://boards.straightdope.com/u/Keeve)\
**Post date:** [March 8, 2009, 2:55am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/10 "2009-03-08T02:55:30Z")

</div>

To me, it looks like you have 14 specific folders you want to look in. I would copy those folders to a new temporary folder, and then just search that entire folder.

No fuss, no muss, no programming.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [March 8, 2009, 3:09pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/11 "2009-03-08T15:09:49Z")

</div>

> [@Keeve](#):
>
> To me, it looks like you have 14 specific folders you want to look in. I would copy those folders to a new temporary folder, and then just search that entire folder.
> 
> No fuss, no muss, no programming.

Ref posts 4 & 5. 🙂

---

<div class="post-metadata">

**Author:** ![Keeve](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@Keeve](https://boards.straightdope.com/u/Keeve)\
**Post date:** [March 8, 2009, 4:51pm UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/12 "2009-03-08T16:51:55Z")

</div>

> [@LSLGuy](#):
>
> Ref posts 4 & 5. 🙂

:smack: how did i miss those? :smack:  
thanx!

---

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [March 9, 2009, 1:33am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/13 "2009-03-09T01:33:29Z")

</div>

> [@Keeve](#):
>
> To me, it looks like you have 14 specific folders you want to look in. I would copy those folders to a new temporary folder, and then just search that entire folder.
> 
> No fuss, no muss, no programming.

Hmm I suppose copying the files would work; although that seems more work than just using the for command.

On a side note; I always forget about the ‘for’ command and how useful that is.

---

<div class="post-metadata">

**Author:** ![Enright3](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/enright3/32/2897_2.png) [@Enright3](https://boards.straightdope.com/u/Enright3)\
**Post date:** [March 9, 2009, 1:34am UTC](https://boards.straightdope.com/t/windows-file-search-question/488581/14 "2009-03-09T01:34:42Z")

</div>

> [@Ximenean](#):
>
> The OP said  
> That’s exactly what mine and other suggestions do. Obviously you can change the 8 to a 9.  
> I got the impression that the OP limited it to 200902 simply because there is no 200903 directory yet.

This is true, I just wanted to look in the last 16-18 months of folders.
