DIR of only specific levels

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

DIR of only specific levels

#1 Post by jugrugs » 24 Mar 2015 11:12

I am trying to get a list of all of the "project.ini" files in a directory on our network. The problem is that all of these files are only 1 or 2 levels deep in that directory, so when i run: "dir /a-d /s /b M:\Cases\*Project.ini > DIR.txt" - it works, but might take all day. There are thousands of folders and millions of files within that directory.
Is there a way to limit how many folders deep it looks? I need it to look up to 2 folders deep, but not any further than that.
Thanks in advance for any help..

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DIR of only specific levels

#2 Post by foxidrive » 24 Mar 2015 12:26

There is no built in method to only search 2 levels deep, you will have to list the directories at least
- to then check for that file in the two levels.

EDIT: The above is not true for all methods, but does apply when using the command in the example above of
"dir /a-d /s /b M:\Cases\*Project.ini > DIR.txt"




Is this over a LAN? You mentioned network.

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#3 Post by jugrugs » 24 Mar 2015 12:33

Yes. Over a LAN in an office environment.

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#4 Post by jugrugs » 24 Mar 2015 15:08

Inside the "M:\Cases\" folder I am working with, there are 1,200+ folders that each contain a "project.ini" file (at that level, or one level further in). I just need a list of the files that exist.
Inside those 1,200+ folders there are thousands of sub-folders which is why I have an issue with this taking too long to scan and list what I need.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: DIR of only specific levels

#5 Post by Aacini » 24 Mar 2015 15:22

Try this:

Code: Select all

@echo off

(for /D %%a in (M:\Cases\*) do (
   dir /B %%a\Project.ini 2> NUL
   for /D %%b in ("%%a") do dir /B "%%b\Project.ini" 2> NUL
)) > DIR.txt

Antonio

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#6 Post by jugrugs » 24 Mar 2015 15:55

That lists most of the folders, but not all of them. There are some folders that do not have the "project.ini" file in there, but I checked, and some that do have the INI file were not listed in the output from your script.
It did run very quickly, so I am guessing it did limit the scan to 2 levels of recursion...
Getting closer!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: DIR of only specific levels

#7 Post by penpen » 24 Mar 2015 18:22

After i have tried reading out of your mind via internet which folders were not listed, but do have the "project.ini" file in there (just joking),
i guess you need more than 2 levels of recursion (probably 3 levels).

If i am wrong, please provide more information.

penpen.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: DIR of only specific levels

#8 Post by Aacini » 24 Mar 2015 20:14

jugrugs wrote:That lists most of the folders, but not all of them. There are some folders that do not have the "project.ini" file in there, but I checked, and some that do have the INI file were not listed in the output from your script.
It did run very quickly, so I am guessing it did limit the scan to 2 levels of recursion...
Getting closer!


Please, read this post in first place.

My solution was written assuming a directory with a structure similar to this one:

Code: Select all

M:\Cases\one\Project.ini
M:\Cases\one\A\Project.ini
M:\Cases\one\A\ABC\Project.ini
M:\Cases\one\A\ABC\XYZ\Project.ini
M:\Cases\one\B
M:\Cases\one\C\Project.ini
M:\Cases\two\A
M:\Cases\two\B\Project.ini
M:\Cases\two\C
M:\Cases\two\D\Project.ini
...
M:\Cases\oneThousandTwoHundred\Project.ini
M:\Cases\oneThousandTwoHundred\A
M:\Cases\oneThousandTwoHundred\A\XYZ\Project.ini
M:\Cases\oneThousandTwoHundred\B\Project.ini

Given previous structure, the program should produce this result:

Code: Select all

M:\Cases\one\Project.ini
M:\Cases\one\A\Project.ini
M:\Cases\one\C\Project.ini
M:\Cases\two\B\Project.ini
M:\Cases\two\D\Project.ini
...
M:\Cases\oneThousandTwoHundred\Project.ini
M:\Cases\oneThousandTwoHundred\B\Project.ini

That is, "Inside the "M:\Cases\" folder there are 1,200+ folders that each contain a "project.ini" file (at that level, or one level further in). I just need a list of the files that exist" as requested.

You are suggesting that some existent files in previous structure were missed from the DIR /B command? It is very unlikely that this may happen (to not said "impossible"). A program always run as it was programmed, there is not such "Getting closer!" thing...

The point that really confuses me is that you first said "all of these files are only 1 or 2 levels deep in that directory", "I need it to look up to 2 folders deep, but not any further than that", but in your last reply you said "It did run very quickly, so I am guessing it did limit the scan to 2 levels of recursion..." ???

Now, read again the recommendations at the link above and post a reply with full details...

Antonio

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#9 Post by jugrugs » 25 Mar 2015 09:29

The results I got from your script gave me this output:
Folder1
Folder2
Folder3
Project.ini
Folder4
Folder5
Folder6
Folder...

It listed tons of the folders (but not all of them), and it did not list them as a full path output with the filename appended like you suggest it should. It only listed 4 project.ini files total (of 2,400+ lines of output) and they were listed by themselves, so I didn't know what folder they belonged to.
I was able to get the results I needed using a robocopy level command.

Thank you very much for your time!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: DIR of only specific levels

#10 Post by Compo » 25 Mar 2015 11:53

…yep something like this:

Code: Select all

ROBOCOPY M:\Cases M: *Project.ini /LEV:2 /S /FP /L /NS /NC /NDL /NJH /NJS /LOG:Dir.txt

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#11 Post by jugrugs » 25 Mar 2015 12:03

Yessir. Actually, yours is the simple version I was looking for. Thanks!!
I got it to work using:

Code: Select all

@echo off
SetLocal

:: script name with extension
SET BAT_NAMEX=%~nx0

:: script name without extension
SET BAT_NAME=%~n0

TITLE %BAT_NAMEX%

:: prepare date/time stamp
FOR /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%i-%%j-%%k-%%l
FOR /f "Tokens=1-2" %%i in ('time /t') do set tm=-%%i-%%j

:: timestamp (dd-mm-yyyy-hh-mm-am)
SET TM=%tm::=-%
SET DTS=%dt%%tm%

::## Set Paths List
SET PATHS_LIST=%BAT_NAME%.list

::## Set Log File
SET LOG_FILE=T:\Project-ini\%BAT_NAME%_%DTS%.log

:: start Robocopying
echo -----
FOR /F "usebackq tokens=1 delims=||" %%a in ("%PATHS_LIST%") DO (
Robocopy.exe "%%a" "%%a" project.ini /nc /nfl /njh /njs /l /e /np /lev:4 /tee /r:3 /log+:"%LOG_FILE%"
echo(
echo -----
)

echo %BAT_NAMEX% has finished...
EndLocal



Thank you everyone for the help!
Last edited by jugrugs on 26 Mar 2015 10:57, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: DIR of only specific levels

#12 Post by Compo » 25 Mar 2015 12:11

jugrugs wrote:FOR /F "usebackq tokens=1 delims=||" %%a in ("%PATHS_LIST%") DO (
Robocopy.exe "%%a" "%%a" project.ini /nc /nfl /njh /njs /l /e /np /lev:4 /tee /r:3 /log+:"%LOG_FILE%"
echo(
echo -----
)
I don't think you can use the same source and destination; and since you're already hard coding a location in T: there's no reason why you cannot use "%%~a" T:

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#13 Post by jugrugs » 25 Mar 2015 12:17

Someone gave me that code, so I'm not sure why it is like that - but it worked. I had to manipulate the output log afterward for what I needed from it though, so the simpler version is much better.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DIR of only specific levels

#14 Post by foxidrive » 25 Mar 2015 20:17

With a tweak and change of tactic - the code below shows the following:

"d:\abc\aaa\Project.ini"
"d:\abc\aaa\bbb\Project.ini"
"d:\abc\backup\123\Project.ini"


When these files are present:

(however files like "project.init" and "project.ini-four" in the first two levels will confuse it)

d:\abc\project.ini
d:\abc\aaa\project.ini
d:\abc\aaa\bbb\project.ini
d:\abc\aaa\bbb\222\project.ini
d:\abc\aaa\bbb\222\ccc & ddd\project.ini
d:\abc\aaa\bbb\222\ccc & ddd\eee\project.ini
d:\abc\backup\123\project.ini
d:\abc\backup\123\bbb\project.ini
d:\abc\backup\123\bbb\ccc & ddd\project.ini
d:\abc\backup\123\bbb\ccc & ddd\eee\project.ini



Code: Select all

@echo off
(for /D %%a in (d:\abc\*) do (
   for %%b in ("%%a\Project.ini?") do echo("%%a\Project.ini"
   for /D %%c in ("%%a\*") do for %%b in ("%%c\Project.ini?") do echo("%%c\Project.ini"
)) > DIR.txt

jugrugs
Posts: 8
Joined: 24 Mar 2015 11:04

Re: DIR of only specific levels

#15 Post by jugrugs » 26 Mar 2015 10:36

foxidrive;
That is pretty awesome. That worked great too (as well as the robocopy options). I just don't understand how.
The literal script I used is:

Code: Select all

@echo off
(for /D %%a in (M:\CASES\*) do (
   for %%b in ("%%a\Project.ini?") do echo("%%a\Project.ini"
   for /D %%c in ("%%a\*") do for %%b in ("%%c\Project.ini?") do echo("%%c\Project.ini"
)) > DIR.txt


If I may ask-
What do the "%%a", "%%b" & "%%c" commands do?
Also, why "for /D"?

Sorry for my ignorance, but I am a novice.

Post Reply