Page 1 of 2 12>
Topic Options
#151012 - 2005-11-04 02:36 PM 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Can anyone spot why I only get one $Project Folder List for each $Source
rather than a list of $Client with all $Location with all $Project tree !
Code:
  
dim $CLIENT[999]
dim $LOCATION[200]
dim $PROJECT[200]
dim $SOURCE[4]
break on

$SOURCE[1] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","SOURCE")
$SOURCE[2] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","DEST")
$SOURCE[3] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC1")
$SOURCE[4] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC2")
if Open(2, "c:\dirlist.txt",5) = 0
for $s= 1 to 4
$CLIENT=Dir($SOURCE[$s])
? $CLIENT
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT))
if not $CLIENT = "." and not $CLIENT = ".." and not $CLIENT = ""

$LOCATION=Dir($SOURCE[$s]+"\"+$CLIENT)
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT+"\"+$LOCATION))
if not $LOCATION = "." and not $LOCATION = ".." and not $LOCATION = ""

$PROJECT=Dir($SOURCE[$s]+"\"+$CLIENT+"\"+$LOCATION)
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT+"\"+$LOCATION+"\"+$PROJECT))
if not $PROJECT = "." and not $PROJECT = ".." and not $PROJECT = ""
$LINE=$source[$s]+","+$client+","+$location+","+$project+@CRLF
? ""+$LINE
;$x = WRITELINE (2,$LINE)
endif
endif
$PROJECT=Dir() ; retrieve next Project
Loop
endif
endif
$LOCATION=Dir() ; retrieve next Location
Loop
if
EndIf
$CLIENT = Dir() ; retrieve next Client
Loop
next
close(2)


Top
#151013 - 2005-11-04 02:43 PM Re: 3 Level ONLY recurse Dir - URGENT
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Each time you start a "Dir()" you will break the previous one.
Top
#151014 - 2005-11-04 02:49 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Will this imply I will have to read each level into an array in order to process the next ?
Top
#151015 - 2005-11-04 02:55 PM Re: 3 Level ONLY recurse Dir - URGENT
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If you want to do it in the same scope, then yes.

Dir() does support an optional index, but it only allows two directory enumerations at the same time (Index=0 or 1).

The alternative is to do the enumeration in a user-defined function and make it truly recursive. Pass a parameter which is the current level and subtract one each time. When the level is 0 just exit the function without enumerating, which will cap the level.

Top
#151016 - 2005-11-04 03:06 PM Re: 3 Level ONLY recurse Dir - URGENT
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
try Bryce's DirPlus() udf. It supports just what you want and some more

Clicky
_________________________



Top
#151017 - 2005-11-04 03:18 PM Re: 3 Level ONLY recurse Dir - URGENT
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
If the UDF doesn't do what you want and you need to re-engineer, here's a simple example to get you started:
Code:
Break ON
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")

$=funRecurse("C:\",3)

Function funRecurse($sDir,$iLevel)
Dim $sEntry

$iLevel=$iLevel-1
$sEntry=Dir($sDir)
While Not @ERROR
If NOT($sEntry="" OR $sEntry="." OR $sEntry="..")
$sEntry=$sDir+"\"+$sEntry
""+$iLevel+": "+$sEntry+@CRLF
If (GetFileAttr($sEntry) & 16) AND $iLevel
funRecurse($sEntry,$iLevel)
EndIf
EndIf
$sEntry=Dir()
Loop
EndFunction


Top
#151018 - 2005-11-04 04:09 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
DirPlus does NOT allow me to easily do only 3 levels as required.
The following I believe should work but for an error insisting there is a missing comma in the Dir(#) ?
Suggestions
Code:

break on
global $CLIENT
global $LOCATION
global $PROJECT
dim $SOURCE[4]
;dim $CLIENT[9999]
;dim $LOCATION[200]
;dim $PROJECT[200]
debug on
$SOURCE[1] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","SOURCE")
$SOURCE[2] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","DEST")
$SOURCE[3] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC1")
$SOURCE[4] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC2")
if Open(2, "c:\dirlist.txt",5) = 0
for $s= 1 to 4
? "#"+$SOURCE[$s]
ClntDir($s)
next
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ClntDir($S)
$CLIENT=Dir(""+$SOURCE[$s],0)
$CC=0
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT))
if not $CLIENT = "." and not $CLIENT = ".." and not $CLIENT = ""
$CC=$CC+1
$CLIENT[$CC] = $CLIENT
LocnDir($S,$CC)
endif
EndIf
$CLIENT = Dir() ; retrieve next Client
Loop
EndFunction
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function LocnDir($S,$CC)
$LOCATION=Dir(""+$SOURCE[$s]+"\"+$CLIENT[$CC],0)
$LC=0
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT[$CC]+"\"+$LOCATION))
if not $LOCATION = "." and not $LOCATION = ".." and not $LOCATION = ""
$LC=$LC+1
$LOCATION[$LC] = $LOCATION
ProjDir($S,$CC,$LC)
endif
endif
$LOCATION=Dir() ; retrieve next Location
Loop
EndFunction
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ProjDir($S,$CC,$LC)
$PROJECT=Dir(""+$SOURCE[$s]+"\"+$CLIENT[$CC]+"\"+$LOCATION[$LC],0)
$PC=0
While Not @ERROR
If (16 & GetFileAttr($SOURCE[$s]+"\"+$CLIENT[$CC]+"\"+$LOCATION[$LC]+"\"+$PROJECT))
if not $PROJECT = "." and not $PROJECT = ".." and not $PROJECT = ""
$PC=$PC+1
$PROJECT[$PC] = $PROJECT
$LINE=$source[$s]+","+$client[$CC]+","+$location[$LC]+","+$project[$PC]+@CRLF
? ""+$LINE
$x = WRITELINE (2,$LINE)
endif
endif
$PROJECT=Dir() ; retrieve next Project
Loop
EndFunction


Top
#151019 - 2005-11-04 04:16 PM Re: 3 Level ONLY recurse Dir - URGENT
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Quote:

DirPlus does NOT allow me to easily do only 3 levels as required.





Quote:

; OPTIONS
; /S Displays files In specified directory and all subdirectories.
; Use a /S# where # is equal to the subfolder depth that you want to recurse.



_________________________



Top
#151020 - 2005-11-04 04:22 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
I already tried that this morning thanks, but found it VERY difficult to be able to output the info to file at the right point, hence the above. Always open to instruction however!
Is there a problem enumerating arrays from within the Dir() ?

Top
#151021 - 2005-11-04 04:24 PM Re: 3 Level ONLY recurse Dir - URGENT
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
no, but it's harder than using the udf which you already told is really hard.
_________________________
!

download KiXnet

Top
#151022 - 2005-11-04 05:01 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
I tried this but it can not handle nesting and /s3 as above did not seem to work - only 1 level displayed.

Code:

break on
global $SOURCE
dim $SOURCE[4]
;debug on
$SOURCE[1] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","SOURCE")
$SOURCE[2] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","DEST")
$SOURCE[3] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC1")
$SOURCE[4] = READPROFILESTRING(@SCRIPTDIR + "\MENU.INI","DRIVES","ARC2")
if Open(2, "c:\dirlist.txt",5) = 0
for $s= 1 to 4
$dir1 = dirplus($SOURCE[$s],"/ad")
; ? dir1
for each $Client in $dir1
$dir2 = dirplus($SOURCE[$s]+"\"+$dir1,"/ad")
; ? dir2
for each $Location in $dir2
$dir3 = dirplus($SOURCE[$s]+"\"+$dir1+"\"+$dir2,"/ad")
; ? dir3
for each $Project in $dir3
$Line=$SOURCE[$s]+"\"+$dir1+"\"+$dir2+"\"+$dir3
? $LINE
; $x = WRITELINE (2,$LINE) ; List of Folders at 3 levels deep
next
next
next
next
endif
close (2)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Function DIRPlus()


Top
#151023 - 2005-11-04 05:10 PM Re: 3 Level ONLY recurse Dir - URGENT
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
With a minor modification the simple example that I gave you will do what I think you want, which is not to iterate a DIR() to three levels, but to get the directories at the third level:
Code:
Break ON
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")

$=funRecurse("C:\",3)

Function funRecurse($sDir,$iLevel)
Dim $sEntry

$iLevel=$iLevel-1
$sEntry=Dir($sDir)
While Not @ERROR
If NOT($sEntry="" OR $sEntry="." OR $sEntry="..")
$sEntry=Join(Split($sDir+"\"+$sEntry,"\\"),"\")
If (GetFileAttr($sEntry) & 16)
If $iLevel
funRecurse($sEntry,$iLevel)
Else
$sEntry+@CRLF
EndIf
EndIf
EndIf
$sEntry=Dir()
Loop
EndFunction


Top
#151024 - 2005-11-04 05:11 PM Re: 3 Level ONLY recurse Dir - URGENT
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
if you are only after files...

Code:
$dir = dirplus($source,'/s3 /a-d')
for each $file in $dir
? $file.name
next



That will only return the files in all sub folders that are 3 deep from the root of $source. Don't forget, that DirPlus() returns an array of FSO object's not strings, hence the $file.name.

Top
#151025 - 2005-11-04 05:18 PM Re: 3 Level ONLY recurse Dir - URGENT
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

if you are only after files...




Other way round - he's after directories, not files.

Top
#151026 - 2005-11-04 05:35 PM Re: 3 Level ONLY recurse Dir - URGENT
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
ahh, ok i think i see the confusion! you want only the files that are located in a folders taht are 3 deep from the root of the source!
Top
#151027 - 2005-11-04 05:58 PM Re: 3 Level ONLY recurse Dir - URGENT
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
i had to go stomp some fires....

but here...
Code:

$source = "v:\"

$folders = dirplus($source,"/ad /s3")
for each $f in $folders
if ubound(split(split($f.path,$source)[1],'\')) = 2
? $f.path
endif
next


Top
#151028 - 2005-11-04 06:05 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
Actually I dont want the files, I need the folder names accurately at the 3rd level cos I need them for a CSV file ....etc
I need to put Folder level 1 in Column A, Folder Level 2 in Column B and Folder Level 3 in Column C.
I am of course doing this last minute now and rapidly funning out of time...

Top
#151029 - 2005-11-04 06:19 PM Re: 3 Level ONLY recurse Dir - URGENT
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Code:

$source = "v:\"

$folders = dirplus($source,"/ad /s3")
for each $f in $folders
select
case ubound(split(split($f.path,$source)[1],'\')) = 0
? "level - 1 " + $f.path
case ubound(split(split($f.path,$source)[1],'\')) = 1
? "level - 2 " + $f.path
case ubound(split(split($f.path,$source)[1],'\')) = 2
? "level - 3 " + $f.path
endselect
next


Top
#151030 - 2005-11-04 06:29 PM Re: 3 Level ONLY recurse Dir - URGENT
MACE Offline
Starting to like KiXtart

Registered: 2004-09-07
Posts: 150
Loc: Manchester UK
This looks great except , as with my earlier attempt, I only get Level 1 printing and no level 2 or 3
Suggestions !

Top
#151031 - 2005-11-04 06:34 PM Re: 3 Level ONLY recurse Dir - URGENT
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
opps... had bugs
Code:

$source = "c:\program files"

$folders = dirplus($source,"/ad /s2")
for each $f in $folders
select
case ubound(split(split($f.path,$source)[1],'\')) = 1
? "level - 1 " + $f.path
case ubound(split(split($f.path,$source)[1],'\')) = 2
? "level - 2 " + $f.path
case ubound(split(split($f.path,$source)[1],'\')) = 3
? "level - 3 " + $f.path
endselect
next


Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.079 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org