Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.clipper.visual-objects > #11850 > unrolled thread

Files in sub directories

Started byfidlergad@gmail.com
First post2018-08-29 04:08 -0700
Last post2018-08-30 15:38 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.clipper.visual-objects


Contents

  Files in sub directories fidlergad@gmail.com - 2018-08-29 04:08 -0700
    Re: Files in sub directories simon@abersoftware.co.uk - 2018-08-29 06:15 -0700
    Re: Files in sub directories fidlergad@gmail.com - 2018-08-29 08:47 -0700
      Re: Files in sub directories Arne Ortlinghaus <Arne.Ortlinghaus@acs.it> - 2018-08-30 15:38 +0200

#11850 — Files in sub directories

Fromfidlergad@gmail.com
Date2018-08-29 04:08 -0700
SubjectFiles in sub directories
Message-ID<e850893c-980b-4e22-8d9e-bd39dd69ae09@googlegroups.com>
How can I list all files in a directory, and all it's sub-directories?

Thanks
Henry

[toc] | [next] | [standalone]


#11852

Fromsimon@abersoftware.co.uk
Date2018-08-29 06:15 -0700
Message-ID<3cfa2d45-3722-47b3-bb99-d0fd6a86c1b9@googlegroups.com>
In reply to#11850
Henry,

This is how I do it...

1. Go through main folder and find all subfolder names. 

subfolderarray = SG_SubFolders([path])

2. Call the Get_Files function to retrieve filenames.

filenamearray = Get_Files([path],[filespec],[subfolderarray]

Any questions let me know.

Simon

This function returns all filenames in path
FUNCTION GetFiles(sPath,sWildCard, aAllFolders)
	LOCAL aReturnFiles, aSubFiles AS ARRAY
	LOCAL dwCount, dwMax AS DWORD
	LOCAL dwOldSize, dwNewSize AS DWORD
	
//	LOCAL fStart, fTime1, fTime2, fTime3 AS FLOAT

//	fStart := Seconds()

	//All files from the main folder should be returned here
	aReturnFiles := SG_Directory( sPath, sWildCard )

//	fTime1 := Seconds() - fStart
	
	//This is a list of all subfolders of the main path - one entry per folder
	//This array is now built in postinit so we only have to do it once
	//aAllFolders := SG_SubFolders(sPath)

//	fTime2 := Seconds() - fStart

	dwMax := ALen(aAllFolders)

//	Message("We have "+NTrim(dwMax)+" subfolders. Path = ["+sPath+"] Wildcard ["+sWildCard+"]")

	//Loop through each of the folders looking for more files and add them to
	//the bottom of the aReturnFiles array
	FOR dwCount := 1 UPTO dwMax
		aSubFiles := SG_Directory( aAllFolders[dwCount], sWildCard )
		IF ALen(ASubFiles) > 0
			dwOldSize := ALen(aReturnFiles)
			dwNewSize := dwOldSize+ALen(aSubFiles)
			ASize(aReturnFiles, dwNewSize)
			ACopy(aSubFiles,aReturnFiles,,,dwOldSize+1)
		ENDIF
	NEXT dwCount

//	fTime3 := Seconds() - fStart


//	Message("Timings : Time 1 :"+NTrim(fTime1)+" Time 2 :"+NTrim(fTime2)+;
//					" Time 3 :"+NTrim(fTime3))

	RETURN aReturnFiles



This returns an array of sub-folder names
FUNCTION SG_SubFolders(cPath)
	//We need to return an array with a list of all sub folders from the main one
	LOCAL aSub AS ARRAY
//, aSubLevel2 AS ARRAY
	LOCAL dwCount, dwMax, dwLevel2, dwMaxLevel2 AS DWORD
	LOCAL sName AS STRING

	aSub := {}

	dwMax := FFCount(cPath+"*.*", FA_DIRECTORY)

	IF dwMax > 0

		FOR dwCount := 1 UPTO dwMax
			sName := FName()
			IF At("D",FAttr2String(FAttrib())) > 0
				IF Left(sName,1) <> "."
					//Message("Adding "+cPath+sName+"\")
					AAdd(aSub,cPath+sName+"\")
				ENDIF
			ENDIF
			FNext()

		NEXT dwCount	

	ENDIF

//Now we need to look in each of these sub folders
	dwMaxLevel2 := ALen(aSub)
	FOR dwLevel2 := 1 UPTO dwMaxLevel2

		dwMax := FFCount(aSub[dwLevel2]+"*.*", FA_DIRECTORY)
	
		IF dwMax > 0
	
			FOR dwCount := 1 UPTO dwMax
				sName := FName()
				IF At("D",FAttr2String(FAttrib())) > 0
					IF Left(sName,1) <> "."
						//Message("Adding "+cPath+sName+"\")
						AAdd(aSub,aSub[dwLevel2]+sName+"\")
					ENDIF
				ENDIF
				FNext()
	
			NEXT dwCount	

		ENDIF
		
	NEXT dwLevel2

	RETURN aSub


This returns an array of all files in a folder (matching the filespec *.doc etc)

FUNCTION SG_Directory(cPath, cWildCard)
	//We need to return an array of arrays with file information
	LOCAL aDir AS ARRAY
	LOCAL dwCount, dwMax AS DWORD

	aDir := {}

	dwMax := FFCount(cPath+cWildCard, FC_NORMAL)

	IF dwMax > 0
		aDir := ArrayNew(dwMax,6)
	
		FOR dwCount := 1 UPTO dwMax
			aDir[dwCount,F_NAME] := FName()
			aDir[dwCount,F_SIZE] := FSize()
			aDir[dwCount,F_DATE] := FDate()
			aDir[dwCount,F_TIME] := FTime()
			aDir[dwCount,F_ATTR] := FAttr2String(FAttrib())
			aDir[dwCount,6] := cPath
			FNext()
		NEXT dwCount	

	ENDIF

	RETURN aDir	



On Wednesday, August 29, 2018 at 12:08:28 PM UTC+1, fidl...@gmail.com wrote:
> How can I list all files in a directory, and all it's sub-directories?
> 
> Thanks
> Henry

[toc] | [prev] | [next] | [standalone]


#11853

Fromfidlergad@gmail.com
Date2018-08-29 08:47 -0700
Message-ID<c2704b98-2090-4498-ad0b-9ecd07776bc4@googlegroups.com>
In reply to#11850
Many thanks
Henry

[toc] | [prev] | [next] | [standalone]


#11854

FromArne Ortlinghaus <Arne.Ortlinghaus@acs.it>
Date2018-08-30 15:38 +0200
Message-ID<pm8s0a$1m24$1@gioia.aioe.org>
In reply to#11853
Hi Henry,
here is a function, that reads the directories into an array and that 
can already do recursive calling.

Arne



function Directory2ArrayEx( cFiles, uAttributes )
//p Zurückgabe von Dateien in ein Array (wie Directory)
//d Im Unterschied zu Directory2Array können mehrere Suchmöglichkeiten 
angegeben mit ; getrennt. \line
//d Als erstes Feld wird der volle Pfadname zurückgegeben und nicht nur 
der Dateiname/Extension \line
//d Directory funktioniert nicht, weil Directory nur die Dateien 
zurückgibt mit dem Archivebit gesetzt.
//d die Attribute für Übergabe sind etwas anders definiert.
//d Es werden automatisch die Directories . und .. ausgeblendet.
//d Struktur des Rückgabe-Arrays ist wie aus Funktion DIRECTORY()  \line
//d Zusätzlich wird noch nach dem Namen aufwärts sortiert
//d aDir[i,1]	Dateiname inklusive Pfad   \line
//d aDir[i,2]	Dateigroesse  \line
//d aDir[i,3]	Datum letztes Schreiben  \line
//d aDir[i,4]	Uhrzeit letztes Schreiben  \line
//d aDir[i,5]	Dateiattribute  \line
//d           Stringkombination aus folgenden Zeichen:  \line
//d           D				Directory  \line
//d				H				Hidden  \line
//d				S				System  \line
//a cFiles - String: kompletter Suchpfad mit * oder ?
//a uAttributes - Einschränkung der Suchkriterien (String oder nil)
//a               Standard ist "FD", Dateien und Directories
//a               Character	Beschreibung
//a               D				Directory
//a               F				Dateien
//a               R Rekursive eine Ebene höher, RR zwei, RRR drei, ... 

//a               andere Parameter werden ignoriert
//r Array: Feld unter Beschreibung
//g Files

local adirrrecursive, aSearch, aDir	as array
local pWin32_Find_Data	is _WINWIN32_FIND_DATA
local irec as dword
local SysTime			is _WINSYSTEMTIME
local hSearch			as ptr
local dFileDate			as date
local cDir, cFileTime			as string
local cFileName as string
local dwAttributes as dword
local cts, cSearch, cAttributes as string
local lrecursive, lSearchDir as logic
local lSearchFiles as logic
local h,m,s as int
local ftlocaltime is _WINFILETIME
local f as real8
local i as dword

Default (@uAttributes, "FD")
aDir := {} // Rückgabe-Array, das letzte Element mit den Dateiattributen 
bleibt vorerst leer, wir brauchen es bisher nicht!
aSearch := String2Array(cFiles, ";")
lSearchDir := false
lSearchFiles := false
lrecursive:= false

if IsString(uAttributes)
	uAttributes := Upper(uAttributes)
	lSearchDir := InStr("D", uAttributes)
	lSearchFiles := InStr("F", uAttributes)
	lrecursive:= InStr("R", uAttributes)
	if lrecursive
	   uAttributes := substr(uAttributes, 1, slen(uAttributes)-1)
	endif
endif
for i := 1 upto ALen(aSearch)
    cSearch := aSearch[i]
    cDir := FAddBackslash(FGetDir(cSearch))
    hSearch := null_ptr
    MemSet(@pWin32_Find_Data, 0, dword(_sizeof(_WINWIN32_FIND_DATA))) 
//X# AO 11/2016
    do while iif ( hSearch == null_ptr, ;
    		(( hSearch := FindFirstFile(String2Psz(cSearch),@pWin32_Find_Data) 
) != null_ptr) ;
    			.and. !Empty(Psz2String(@pWin32_Find_Data.cFileName)),;
    		FindNextFile(hSearch,@pWin32_Find_Data);
    		)
    	cFileName := Psz2String(@pWin32_Find_Data.cFileName)
    	// Datum und Uhrzeit muessen wir noch rauskriegen, also erstmal 
konvertieren

    	//von UTC-Time nach lokaler Zeit
    	FileTimeToLocalFileTime(;
        @pWin32_Find_Data.ftLastWriteTime,	;// pointer to UTC file time 
to convert
        @ftlocaltime 	;// pointer to converted file time
       )
       //Zerlegen der Zeit
    	FileTimeToSystemTime(;					// Win-API!
        	@ftlocaltime,;	//
    	    @SysTime )
    	// Einzelwerte in VO-Datumsformat umwandeln
// Todo 100317 AO 2016-07-13 15:57 Vulcan: Umstellung, Fehlersuche, ...
//   	dFileDate := CToD( LTrim(Str3(systime.wday,2,0)) + "." + 
LTrim(Str3(systime.wmonth,2,0)) + "." + Str3(systime.wyear,4,0))
    dFileDate := makedate(systime.wyear, systime.wmonth, systime.wday)


    	h := systime.wHour
    	m := systime.wMinute
    	s := systime.wSecond
    	cFileTime := StrZero(h,2) + ":" + StrZero(m,2) + ":" + StrZero(s,2)

    	dwAttributes := pWin32_Find_Data.dwFileAttributes
    	cAttributes := ""
    	if _and (dwAttributes, FILE_ATTRIBUTE_DIRECTORY) != 0
    		cAttributes += "D"
    	endif
    	if _and (dwAttributes, FILE_ATTRIBUTE_HIDDEN) != 0
    		cAttributes += "H"
    	endif
    	if _and (dwAttributes, FILE_ATTRIBUTE_SYSTEM) != 0
    		cAttributes += "S"
    	endif
    	// Todo 104362 Job für nächtliche Kontrolle DB-Skript-Dateien und 
Setzen des "Readonly"-flags
    	if _and (dwAttributes, FILE_ATTRIBUTE_READONLY) != 0
    		cAttributes += "R"
    	endif


    	cFileName := AllTrim(cFileName)
    	if (lSearchDir .and. InStr("D", cAttributes) .and. 
!InList(cFileName, ".", ".."))
          f := pWin32_Find_Data.nFileSizeLow
          if pWin32_Find_Data.nFileSizeHigh > 0
             f += (1.0*pWin32_Find_Data.nFileSizeHigh)*MAXDWORD
          endif   			 

          // Todo 106250 AO 2017-05-30 15:46 Verbesserungen 
Standardfunktionalitäten 2017
          cts := substr(maketimestampbase(dfiledate), 1, 11)+cFileTime
    	    AAdd(aDir,{cDir + cFileName,;                 				// FileName
    					f,;			// FileSize
    					dFileDate,;
    					cFileTime,;
    					cAttributes, ;
    					cts})
          if lrecursive
             adirrrecursive := Directory2ArrayEx( cDir + 
cFileName+"\"+fgetfilenameandext(cFiles), uAttributes )
             for irec := 1 upto alen(adirrrecursive)
                AAdd (adir, adirrrecursive[irec])
             next
          endif   					
    	endif
    	if (lSearchFiles .and. !InStr("D", cAttributes))
          f := pWin32_Find_Data.nFileSizeLow
          if pWin32_Find_Data.nFileSizeHigh > 0
             f += (1.0*pWin32_Find_Data.nFileSizeHigh)*MAXDWORD
          endif
          // Todo 106250 AO 2017-05-30 15:46 Verbesserungen 
Standardfunktionalitäten 2017
          cts := substr(maketimestampbase(dfiledate), 1, 11)+cFileTime
    	    AAdd(aDir,{cDir + cFileName,;                 				// FileName
    					f,;			// FileSize
    					dFileDate,;
    					cFileTime,;
    					cAttributes, ;
    					cts})
    	endif

    enddo
    if hSearch != null_ptr
    	FindClose(hSearch)
    endif
next

ASortTwoDim (adir, 1, true)
return (aDir)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.clipper.visual-objects


csiph-web