folder search problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dibbler
Posts: 5
Joined: 21 Jun 2011 10:17

folder search problem

#1 Post by Dibbler » 21 Jun 2011 10:20

I have a 1/4 million folders to search through for all the instances of a folder called "Record drawings n Documents", once found I need to DIR the folder & content using /s /b to give a bare format & recurs through all the files folders downstream & then use >> to add the findings to a txt file.

unfortunately my knowledge of DOS is light, so I've quickly got stuck...I've got this far, but it doesn’t work...


Code: Select all

@echo off

for /d %%a in (Record drawings n Documents) do Dir "%%a" >> "C:\Users\ben\Desktop\New folder\test.txt" /s /b


Pause


I can get the search to work on a single word but not Record drawings n Documents and I can get the /s switch to work but I can’t make it report anything other than just the folder I was searching on.

Help!

Thanks in advance

Dibbler

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: folder search problem

#2 Post by Ed Dyreen » 21 Jun 2011 11:07


try this:
Dir "%%a" /s /b >> "C:\Users\ben\Desktop\New folder\test.txt"
or
>> "C:\Users\ben\Desktop\New folder\test.txt" Dir "%%a" /s /b
but NOT THIS:
Dir "%%a" >> "C:\Users\ben\Desktop\New folder\test.txt" /s /b

try not to use these >> << between a command and it's parameters. :wink:

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: folder search problem

#3 Post by Acy Forsythe » 21 Jun 2011 13:04

I think this is what you want...

This will search through every directory starting at root \ looking for your directory name.

The result (you can try it with DIR /s "My directory Name") will give you a list of every directory that contains your directory.

The for loop will populate that parent directory into %%a and then do a DIR /s /b "%%a\My directory Name" and output it to a the file you specify.

Unfortunately the For loop is not working... The DIR command works through the command prompt though, just not working iin conjunction with the for loop.

Code: Select all

@echo off

CD \

for /F "tokens=3 delims= " %%a in ('DIR /S "Batch" ^| FIND "Directory"') do Dir "%%a\Batch" /s /b >> "C:\test.txt"
exit /b


EDIT I also realized that it may not work with spaces in directories... Let me see if I can fix it...

EDIT2 - Amazing how missing a single space can cause lots of frustration :) The FOR loop works now, needed a space after delims=

I also removed the SET command because I wasn't using it and I as also afraid it would conflict with my FIND text.

Dibbler
Posts: 5
Joined: 21 Jun 2011 10:17

Re: folder search problem

#4 Post by Dibbler » 22 Jun 2011 02:19

Ed Dyreen...

Thanks for posting, I tried putting the switches just about everywhere to get the /s to work, which it didn’t! That’s just where they ended up, but I take your point & will make sure they sit with the command in future, thanks for the heads up.

Acy Forsythe...
The code runs without error, but also without any output, this could be (read probably is) user error, I'm struggling a little to unpick the code to understand what it’s doing.

I think it’s something like...

for | all folders |dir folders called "Directory" | dir the found folders to txt file

- for /F "tokens=3 delims= I think it states "for folders only" from what google has found, have I got the gist right?

- I cant work out exactly what you're doing in the middle search...

. - what is Batch & what is Directory (I cant work out why there are 2 strings)

. - what does ^| do ? (prob part of the previous question)

- why Dir "%%a\Batch" not Dir "%%a" you must have a reason but i can’t see it ( I can see it must be something to do with the string batch in the middle section but can’t make the link yet.



Sorry for being thick, I do a bit of sudo coding (generally VB.net) with external coders but I desperately need to get better at quick dirty batch files. However I can’t find a good resource for learning, you don’t know of one do you? Not DOS because I'll just get confused as to what works, just the Command window & batch files within Win 7. I'm more than happy to pay for a book if one exists.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: folder search problem

#5 Post by dbenham » 22 Jun 2011 02:45

This is partially tested - I think it may be solid. Run it from where you want to start looking for your directory. This may take a long time to run.

Code: Select all

@echo off
setlocal
set outfile="C:\Users\ben\Desktop\New folder\test.txt"
(for /f "delims=" %%d in ('dir /b /s /ad "Record drawings n Documents"') do dir /s /b "%%d") >%outfile%
The danger I see is if "Record drawings n Documents" appears in the directory tree of another "Record drawings n Documents" folder. If this happens then the subdirectory tree will be printed twice.


I think the following untested code will eliminate duplicate trees.

Code: Select all

@echo off
setlocal
set outfile="C:\Users\ben\Desktop\New folder\test.txt"
set "f=Record drawings n Documents"
(for /f "delims=" %%d in ('dir /b /s /ad "%f%" | findstr /v /i /r /c:"[\\]%f%[\\].*[\\]%f%$" /c:"[\\]%f%[\\]%f%$"') do dir /s /b "%%d") >%outfile%


Dave Benham
Last edited by dbenham on 22 Jun 2011 06:56, edited 1 time in total.

Dibbler
Posts: 5
Joined: 21 Jun 2011 10:17

Re: folder search problem

#6 Post by Dibbler » 22 Jun 2011 04:28

The corrected code fell straight through without creating the txt file and I couldn’t find the fault.

The first code worked but created an output file called "Output" with no file extension. I couldn’t convince it to use the value set so I replaced "output" with a file name and it now works, not elegant but functional.

Thanks for taking the time to write it, I can’t follow all of it but it works & I now have something to reverse engineer to learn more.

Thanks Dibbler



dbenham wrote:This is partially tested - I think it may be solid. Run it from where you want to start looking for your directory. This may take a long time to run.

Code: Select all

@echo off
setlocal
set outfile="C:\Users\ben\Desktop\New folder\test.txt"
(for /f "delims=" %%d in ('dir /b /s /ad "Record drawings n Documents"') do dir /s /b "%%d") >outfile
The danger I see is if "Record drawings n Documents" appears in the directory tree of another "Record drawings n Documents" folder. If this happens then the subdirectory tree will be printed twice.


I think the following untested code will eliminate duplicate trees.

Code: Select all

@echo off
setlocal
set outfile="C:\Users\ben\Desktop\New folder\test.txt"
set "f=Record drawings n Documents"
(for /f "delims=" %%d in ('dir /b /s /ad "%f%" | findstr /v /i /r /c:"[\\]%f%[\\].*[\\]%f%$" /c:"[\\]%f%[\\]%f%$"') do dir /s /b "%%d") >outfile


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: folder search problem

#7 Post by dbenham » 22 Jun 2011 06:59

Dibbler wrote:The first code worked but created an output file called "Output" with no file extension.

That's because I forgot to put the percents around the outfile. I edited my post above to correct it.

Which version did you use? The 1st one with the potential duplicate trees, or the 2nd one?

Dave Benham

Dibbler
Posts: 5
Joined: 21 Jun 2011 10:17

Re: folder search problem

#8 Post by Dibbler » 22 Jun 2011 07:55

The first version is currently running on the drive, I tested it in a local folder first & then once proven I set it up on the full dataset.

The searched folder name should never repeat in the downstream folders, so I'm happy to take the risk.

It’s not long finished (about 1 hour 40 to run through). The only down side to the code is that it dumps all the data into the txt file at the end, rather than on the fly, so you don’t know how far in it’s gone, but that’s a very small issue.

Just saved me days of work

Thanks again for getting it to go.

Dibbler


dbenham wrote:
Dibbler wrote:The first code worked but created an output file called "Output" with no file extension.

That's because I forgot to put the percents around the outfile. I edited my post above to correct it.

Which version did you use? The 1st one with the potential duplicate trees, or the 2nd one?

Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: folder search problem

#9 Post by Acy Forsythe » 22 Jun 2011 08:10

Dibbler,

Sorry for the confusion, I used "Batch" in my test script, so I apologize for the confusion, you can replace "Batch" with "Records Drawings n Documents" in both places.

But I will run down the code for you:


run that DIR command from the C:\ prompt...

DIR /s "Records Drawings n Documents"

Review the results, then add the find portion:

DIR /s "Recordings Drawings n Documents" | FIND "Directory"

The ^ character in that string is only necessary in the batch file, from a command prompt you can just type the pipe character.

That produces a list of ALL of the parent directories that contain a file or folder called "Records Drawings n Documents"

So if you compare the output of the two commands above, you'll see what the find is doing for us that the DIR was not.

We already knew WHAT we were looking for, "Records Drawings n Documents" the DIR command tells us WHERE to look for it at. And the FIND command breaks that down into a list and gets rid of most of the useless bits we didn't need to know about.

So now we have a list that looks like this:

Directory of C:\Users\Me\Folder
Directory of C:\Users\Me\Documents
Directory of C:\Users\Me\Data

Now the for loop will go through each one of those lines and set %%a to whatever we tell it to set a%% to. We use the tokens and delims for that.

We're going to set a space as our delimiter and multiple delimeters are counted as 1.

Now tokens tells the for loop what data to use to set a%% and b%% and c%% etc... but we're only using 1 variable so we only need 1 token. We want the path part, the C:\Users\etc... part of the line. So we use Tokens=3 we want the 3rd token. If we wanted 3 tokens, we'd use Tokens=1-3 to specify a range, but we specifically want token number 3.

The tokens are the peices of data seperated by the delimiter, so Directory is Token 1, of is token 2, and C:\users\Me\Data is token3.

We could have also done this:

For /F "tokens=1-3 delims= " %%a in (Our command that I'm too lazy to retype right now) DO echo %%c

%%a would be set at token1, %%b would be set at token2 and %%c would be set as token3 which is the token we wanted.

I think you actually have a handle on the rest of it but here it is:

.... DO Dir "%%a\Records Drawings n Documents" /s /b >> "C:\test.txt"

%%a is actually equal to C:\Users\Me\Folder

You're adding the \Records Drawings n Documents part and then redirecting the output of that second DIR command to C:\Test.txt which is also what I was using to test, you can change that to whatever you want.

The next pass through the FOR loop, %%a will be = C:\Users\Me\Documents and so on and so forth.

The results are a list of fully qualified path names for every file in every "records Drawings n Documents" folder on drive C:

My command might be easier to read, but I like Dave's better, findstr is much more capable than FIND, but I'm a creature of habbit.

Sorry for being thick, I do a bit of sudo coding (generally VB.net) with external coders but I desperately need to get better at quick dirty batch files. However I can’t find a good resource for learning, you don’t know of one do you? Not DOS because I'll just get confused as to what works, just the Command window & batch files within Win 7. I'm more than happy to pay for a book if one exists.


I know of several good resources...
1. This site.
2. http://technet.microsoft.com/en-us/libr ... 2390(WS.10).aspx
3. http://www.robvanderwoude.com/news.php

The 3rd site has scripts dating all the way back to DOS and OS/2, but there is also a lot of newer stuff and they typically specify what versions it works in. If it says NT/XP or WIN2k then it still works. A lot of the older stuff still works too though.

EDIT: Just noticed that Dave's first script does the exact same thing as mine, the |FIND "string" is replaced with adding the /b /ad switches on the directory command and eliminates the need to use tokens.

Dibbler
Posts: 5
Joined: 21 Jun 2011 10:17

Re: folder search problem

#10 Post by Dibbler » 24 Jun 2011 01:57

Thanks to everyone who took the time to help, I do the same on forums where I’m the expert & this has reminded me why I do it.

The code has done its job & saved me a bag load of time & stress.

Cheers, Dibbler

:EDIT:

And thanks for the code explanations & the links to the resources, rest assured I will be using them all.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: folder search problem

#11 Post by orange_batch » 24 Jun 2011 03:24

I recommend http://ss64.com/nt/ , short of getting into VB Script and such.

More: viewtopic.php?f=3&t=1815&p=7223&hilit=url#p7223

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: folder search problem

#12 Post by Ed Dyreen » 29 Jun 2011 13:27


for beginners to intermidiates I recommend
'MS-DOS/MSDOS Batch File Command Reference - Allenware'
http://www.allenware.com/icsw/icswref.htm

for experts I recommend
'scriptingpros.com' Ed's section about DOS
http://www.scriptingpros.com/index.php
or just browse jeb's posts on this forum. He is the best batcher you'll ever meet !

Or am I posting immature spam AGAIN ? :P

Code: Select all

@Color orange
No, I'm not implying I'm an expert :twisted: I'm an absolute beginner alright !, everybody happy now ?

Post Reply