Thursday, December 31, 2015

Get Directories or Folder Space in Visual Foxpro

A (rough) tool written in VFP called DirSpace where the user can point to a folder and it sums up all of the space used in the subdirectories from that point and below. The results are a simple browse window. I use it all the time in helping me decide what to clean up by finding where all of my drive space is going. Author: Kevin Cully  Other Open Source License 2.3K Last updated: 2007.08.30

SET TALK OFF
CLOSE DATA ALL
CREATE CURSOR DirSpace (DirPath C(254), FilesSize N(15,0), FilesBelow N(15,0), FileCount N(15,0), LDirPath N(15,0), Done L)
PRIVATE lcStartDir, lcOrigDir
lcStartDir = GETDIR() && This is causing a problem by returning the results in all uppercase.

lcOrigDir = SYS(5) + CURDIR()
=GetDirSpace(lcStartDir)
CD '&lcOrigDir'
WAIT CLEAR

INDEX ON LDirPath TAG LDirPath DESC
SET ORDER TO LDirPath

?? CHR(7)

SELECT DirPath, FilesSize FROM DirSpace INTO CURSOR tDirSpace ;
WHERE .T. ;
ORDER BY DirPath

SELECT DirSpace
SCAN
lcDirPath = ALLTRIM( DirSpace.DirPath )
WAIT WINDOW "Summing file space for: " + CHR(13) + lcDirPath NOWAIT

SELECT tDirSpace
SUM FilesSize TO lnFilesSize FOR lcDirPath $ tDirSpace.DirPath

SELECT DirSpace
REPLACE DirSpace.FilesBelow WITH lnFilesSize, ;
DirSpace.Done WITH .T.
ENDSCAN
WAIT CLEAR

INDEX ON FilesBelow TAG FilesBelow DESC
SET ORDER TO FilesBelow

BROWSE LAST FIELDS DirPath, FilesSize, FilesBelow, FileCount
?? CHR(7)

********************
FUNCTION GetDirSpace
PARAMETER pcDirectory
PRIVATE laDirArray, lnX, lnFileSizes, lnFileCount, lcDirectory
LOCAL llRetVal
llRetVal = .F.
DIMENSION laDirArray[1,1]

lcDirectory = '"' + pcDirectory + '"'
* ? TIME(), lcDirectory, LEN(lcDirectory)
TRY
CD (lcDirectory)

INSERT INTO DirSpace (DirPath, LDirPath) VALUES (pcDirectory, LEN(ALLTRIM(pcDirectory)) )
lnElements = ADIR(laDirArray, "*.*", "AHRS")

IF lnElements > 0
=ASORT(laDirArray, 2)
WAIT WINDOW "Processing directory: " + pcDirectory NOWAIT
ENDIF

*** Get File Sizes ***
lnFileSizes = 0
lnFileCount = 0
FOR lnX = 1 TO lnElements
IF NOT "D" $ laDirArray[lnX, 5]
lnFileSizes = lnFileSizes + laDirArray[lnX, 2]
lnFileCount = lnFileCount + 1
ENDIF
ENDFOR
REPLACE DirSpace.FilesSize WITH lnFileSizes, ;
DirSpace.FileCount WITH lnFileCount
*** DEBUGOUT lnFileSizes

*** Recursive Call With Directories ***
lnElements = ADIR(laDirArray, "*.*", "D")
FOR lnX = 1 TO lnElements
IF "D" $ laDirArray[lnX, 5]
lcSubDirectory = laDirArray[lnX, 1]
IF lcSubDirectory # "." AND lcSubDirectory # ".."
=GetDirSpace(pcDirectory + lcSubDirectory + "\")
ENDIF
ENDIF
ENDFOR
llRetVal = .T.
CATCH
? TIME(), [>>>Problem changing to directory], lcDirectory

ENDTRY
RETURN llRetVal




No comments:

Post a Comment