1. Directory info?
- Posted by DonCCole Sep 18, 2008
- 1117 views
Hello Everybody,
How can I total number of bytes in a directory or drive?
And Bytes free?
Don Cole A Bug is an un-documentedfeature. A Feature is a documented Bug.
2. Re: Directory info?
- Posted by DonCCole Sep 20, 2008
- 1099 views
Hello Everybody,
How can I total number of bytes in a directory or drive?
And Bytes free?
Don Cole A Bug is an un-documentedfeature. A Feature is a documented Bug.
I miss spoke here it should say.
Hello Everybody,
How can I *get* the total number of bytes in a directory or drive?
And Bytes free?
Don Cole A Bug is an un-documentedfeature. A Feature is a documented Bug.
3. Re: Directory info?
- Posted by DanM Sep 20, 2008
- 1124 views
Don,
There's a demo with Win32Lib called "WalkDir", which might be overkill for what you want, but it should be adaptable to output file/directory sizes for summing; right now it just shows all dirs/files, but you could certainly tweak it for file sizes. I think I did something like that to show all dirs ranked by size on a drive, but I'm not sure where it is right now.
DanM
4. Re: Directory info?
- Posted by DonCCole Sep 20, 2008
- 1190 views
- Last edited Sep 21, 2008
Don,
There's a demo with Win32Lib called "WalkDir", which might be overkill for what you want, but it should be adaptable to output file/directory sizes for summing; right now it just shows all dirs/files, but you could certainly tweak it for file sizes. I think I did something like that to show all dirs ranked by size on a drive, but I'm not sure where it is right now.
DanM
Thanks Dan for that idea.
In DOS or Command prompt if I type 'dir' I get:
08/30/2008 09:53 PM <DIR> .
08/30/2008 09:53 PM <DIR> ..
08/30/2008 09:53 PM <DIR> Start Menu
08/30/2008 09:54 PM <DIR> My Documents
08/30/2008 09:54 PM <DIR> Favorites
08/30/2008 09:23 PM <DIR> Desktop
08/31/2008 09:53 PM 877 AutoHotkey.ini
09/19/2008 09:40 PM 2,097,152 ntuser.dat
2 File(s) 2,098,029 bytes used space
6 Dir(s) 1,305,751,552 bytes free free space
In windows if I click on my Computer and then right click a drive I get:
A pie chart that shows,
used space
free space
capacity
In euphoria dos,
d = dir(current_dir())
d might have:
{ {".", "d", 0 1994, 1, 18, 9, 30, 02},
{"..", "d", 0 1994, 1, 18, 9, 20, 14},
{"fred", "ra", 2350, 1994, 1, 22, 17, 22, 40},
{"sub", "d" , 0, 1993, 9, 20, 8, 50, 12}
}
d[3][D_NAME] would be "fred" name
d[3][D_S1ZE] would be "fred" size
this gives me total size of all the files, but not free space or used space
Let us just forget about sub directory right now as I have that covered with walk_dir.
What I want to find in a used space, free space and capacity.
Of course *capacity* = free space + used space.
I need two of these factors to figure the third.
How can I do this with euphoria?
5. Re: Directory info?
- Posted by DanM Sep 20, 2008
- 1088 views
- Last edited Sep 21, 2008
I don't know if this will work or not, but given that the dos/command prompt will allow dir to get what you want, maybe you could use system() or system_exec() with the dir command to retrieve the dir info & then parse it for the parts you need?
Dan
6. Re: Directory info?
- Posted by DonCCole Sep 21, 2008
- 1061 views
I don't know if this will work or not, but given that the dos/command prompt will allow dir to get what you want, maybe you could use system() or system_exec() with the dir command to retrieve the dir info & then parse it for the parts you need?
Dan
Thanks again Dan.
I thought of that but how would retrieve the data? Say into a .dat file.
7. Re: Directory info?
- Posted by euphoric (admin) Sep 21, 2008
- 1051 views
I thought of that but how would retrieve the data? Say into a .dat file.
In your program do
system( "dir > myfile.txt", 2 )Then open that file and parse the line with "Dir(s)" on it for the "bytes free" info.
Here's a sample of what you'll get in myfile.txt:
C:\Documents and Settings\HP_Administrator>dir Volume in drive C is HP_PAVLON Volume Serial Number is CD14-1DE0 Directory of C:\Documents and Settings\HP_Administrator 09/20/2008 12:34 AM <DIR> . 09/20/2008 12:34 AM <DIR> .. 09/20/2008 10:27 PM <DIR> Desktop 06/08/2008 09:28 PM 199 ex.err 03/01/2008 08:21 PM <DIR> Favorites 09/02/2008 05:45 PM <DIR> My Documents 03/01/2008 06:50 PM <DIR> Start Menu 03/08/2008 06:54 PM <DIR> Temp 05/25/2006 04:56 PM <DIR> WINDOWS 2 File(s) 60,943 bytes 11 Dir(s) 182,539,636,736 bytes free
8. Re: Directory info?
- Posted by just_me Sep 21, 2008
- 1066 views
On Windows:
include machine.e include dll.e include get.e
constant SECTORS_PER_CLUSTER = 1, BYTES_PER_SECTOR = 2, NUMBER_OF_FREE_CLUSTERS = 3, TOTAL_NUMBER_OF_CLUSTERS = 4,
TOTAL = 1, USED = 2, AVAILABLE = 3,
KERNEL32 = open_dll("kernel32.dll"),
xGetDiskFreeSpaceA = define_c_func(KERNEL32, "GetDiskFreeSpaceA", {C_CHAR, C_POINTER, C_POINTER, C_POINTER, C_POINTER}, C_INT)
sequence sDiskSpace integer void
function GetDiskFreeSpaceA(object xPath) sequence sDiskSpace atom aPath atom aSectorsPerCluster, aBytesPerSector, aNumberOfFreeClusters, aTotalNumberOfClusters
sDiskSpace = {0, 0, 0, 0}
if sequence(xPath) then aPath = allocate_string(xPath) else aPath = NULL end if
aSectorsPerCluster = allocate(4) aBytesPerSector = allocate(4) aNumberOfFreeClusters = allocate(4) aTotalNumberOfClusters = allocate(4)
if c_func(xGetDiskFreeSpaceA, {aPath, aSectorsPerCluster, aBytesPerSector, aNumberOfFreeClusters, aTotalNumberOfClusters}) then sDiskSpace[SECTORS_PER_CLUSTER] = peek4s(aSectorsPerCluster) sDiskSpace[BYTES_PER_SECTOR] = peek4s(aBytesPerSector) sDiskSpace[NUMBER_OF_FREE_CLUSTERS] = peek4s(aNumberOfFreeClusters) sDiskSpace[TOTAL_NUMBER_OF_CLUSTERS] = peek4s(aTotalNumberOfClusters) end if
if sequence(xPath) then free(aPath) end if free(aSectorsPerCluster) free(aBytesPerSector) free(aNumberOfFreeClusters) free(aTotalNumberOfClusters)
return sDiskSpace end function
function GetDiskSpaceInBytes(object xPath) sequence sBytes, sTmpBytes sBytes = {0, 0, 0} sTmpBytes = GetDiskFreeSpaceA(xPath) sBytes[TOTAL] = sTmpBytes[BYTES_PER_SECTOR] * sTmpBytes[SECTORS_PER_CLUSTER] * sTmpBytes[TOTAL_NUMBER_OF_CLUSTERS] sBytes[AVAILABLE] = sTmpBytes[BYTES_PER_SECTOR] * sTmpBytes[SECTORS_PER_CLUSTER] * sTmpBytes[NUMBER_OF_FREE_CLUSTERS] sBytes[USED] = sBytes[TOTAL] - sBytes[AVAILABLE] return sBytes end function
sDiskSpace = GetDiskSpaceInBytes("C:")
printf(1, "Total: %d\nUsed: %d\nAvailable: %d", {sDiskSpace[TOTAL], sDiskSpace[USED], sDiskSpace[AVAILABLE]})
void = wait_key()
JG
{{{
}}}
9. Re: Directory info?
- Posted by just_me Sep 21, 2008
- 1078 views
Again:
On Windows:
include machine.e include dll.e include get.e constant SECTORS_PER_CLUSTER = 1, BYTES_PER_SECTOR = 2, NUMBER_OF_FREE_CLUSTERS = 3, TOTAL_NUMBER_OF_CLUSTERS = 4, TOTAL = 1, USED = 2, AVAILABLE = 3, KERNEL32 = open_dll("kernel32.dll"), xGetDiskFreeSpaceA = define_c_func(KERNEL32, "GetDiskFreeSpaceA", {C_CHAR, C_POINTER, C_POINTER, C_POINTER, C_POINTER}, C_INT) sequence sDiskSpace integer void function GetDiskFreeSpaceA(object xPath) sequence sDiskSpace atom aPath atom aSectorsPerCluster, aBytesPerSector, aNumberOfFreeClusters, aTotalNumberOfClusters sDiskSpace = {0, 0, 0, 0} if sequence(xPath) then aPath = allocate_string(xPath) else aPath = NULL end if aSectorsPerCluster = allocate(4) aBytesPerSector = allocate(4) aNumberOfFreeClusters = allocate(4) aTotalNumberOfClusters = allocate(4) if c_func(xGetDiskFreeSpaceA, {aPath, aSectorsPerCluster, aBytesPerSector, aNumberOfFreeClusters, aTotalNumberOfClusters}) then sDiskSpace[SECTORS_PER_CLUSTER] = peek4s(aSectorsPerCluster) sDiskSpace[BYTES_PER_SECTOR] = peek4s(aBytesPerSector) sDiskSpace[NUMBER_OF_FREE_CLUSTERS] = peek4s(aNumberOfFreeClusters) sDiskSpace[TOTAL_NUMBER_OF_CLUSTERS] = peek4s(aTotalNumberOfClusters) end if if sequence(xPath) then free(aPath) end if free(aSectorsPerCluster) free(aBytesPerSector) free(aNumberOfFreeClusters) free(aTotalNumberOfClusters) return sDiskSpace end function function GetDiskSpaceInBytes(object xPath) sequence sBytes, sTmpBytes sBytes = {0, 0, 0} sTmpBytes = GetDiskFreeSpaceA(xPath) sBytes[TOTAL] = sTmpBytes[BYTES_PER_SECTOR] * sTmpBytes[SECTORS_PER_CLUSTER] * sTmpBytes[TOTAL_NUMBER_OF_CLUSTERS] sBytes[AVAILABLE] = sTmpBytes[BYTES_PER_SECTOR] * sTmpBytes[SECTORS_PER_CLUSTER] * sTmpBytes[NUMBER_OF_FREE_CLUSTERS] sBytes[USED] = sBytes[TOTAL] - sBytes[AVAILABLE] return sBytes end function sDiskSpace = GetDiskSpaceInBytes("C:") printf(1, "Total: %d\nUsed: %d\nAvailable: %d", {sDiskSpace[TOTAL], sDiskSpace[USED], sDiskSpace[AVAILABLE]}) void = wait_key()
JG
10. Re: Directory info?
- Posted by DerekParnell (admin) Sep 21, 2008
- 1090 views
Again:
Do I have permission to include this into the v4.0 standard library? And if so, if you want acknowledgement, what name for you should we use in the docs?
11. Re: Directory info?
- Posted by ne1uno Sep 21, 2008
- 1068 views
On Windows:
GetDiskFreeSpace has a problem with disks bigger than 2gig, I modified your program to use GetDiskFreeSpaceEx
not really tested that well and output needs work to divide by 1024 or 1000 to get Gig's or Megs
--GetDiskFreeSpace --using the as yet unreleased eu4 include std/machine.e include std/dll.e include std/get.e include std/console.e include std/text.e include "std/sequence.e" constant FREEBYTESAVAILABLETOCALLER = 1 , TOTALNUMBEROFBYTES = 2 , TOTALNUMBEROFFREEBYTES = 3 ,KERNEL32 = open_dll("kernel32.dll") ,xGetDiskFreeSpaceExA = define_c_func(KERNEL32 , "GetDiskFreeSpaceExA" , {C_CHAR, C_POINTER, C_POINTER, C_POINTER} , C_INT) sequence sDiskSpace integer void --~ typedef union _ULARGE_INTEGER { --~ struct { --~ DWORD LowPart; --~ DWORD HighPart; --~ }; --~ DWORDLONG QuadPart; --~ } ULARGE_INTEGER; public function comafy(object n, sequence mode="%f", object sep=",") --could get seperator from locale? --maybe should allow comafy non numerics? --should allow already spaced or underlined seperated numbers? sequence s="" object so --assumne it is an atom s = sprintf(mode, n) --reverse & chunk it don't add extra coma if <3 digits don't count decimal point --don't get confused if locale is not using decimal point or coma so= split(s, '.') s = reverse(join(chunk(reverse(so[1]), 3), sep) ) if length(so)>1 then s &= "." & so[2] end if return s end function function GetDiskFreeSpaceExA(object xPath) sequence sDiskSpace atom aPath atom aFreeBytesAvailableToCaller , aTotalNumberOfBytes , aTotalNumberOfFreeBytes sDiskSpace = {0, 0, 0, 0} if sequence(xPath) then aPath = allocate_string(xPath) else aPath = NULL end if aFreeBytesAvailableToCaller = allocate(4) aTotalNumberOfBytes = allocate(4) aTotalNumberOfFreeBytes = allocate(4) if c_func(xGetDiskFreeSpaceExA , {aPath, aFreeBytesAvailableToCaller , aTotalNumberOfBytes, aTotalNumberOfFreeBytes}) then -- values obtained by this function are of type ULARGE_INTEGER. -- Be careful not to truncate these values to 32 bits. sDiskSpace[FREEBYTESAVAILABLETOCALLER] = peek4s(aFreeBytesAvailableToCaller) + (#FFFFFFFF * peek4s(aFreeBytesAvailableToCaller+4)) sDiskSpace[TOTALNUMBEROFBYTES] = peek4s(aTotalNumberOfBytes) + (#FFFFFFFF * peek4s(aTotalNumberOfBytes+4)) sDiskSpace[TOTALNUMBEROFFREEBYTES] = peek4s(aTotalNumberOfFreeBytes) + (#FFFFFFFF * peek4s(aTotalNumberOfFreeBytes+4)) end if if aPath then free(aPath) end if free(aFreeBytesAvailableToCaller) free(aTotalNumberOfBytes) free(aTotalNumberOfFreeBytes) return sDiskSpace end function sequence path="C:" --should check exists sDiskSpace = GetDiskFreeSpaceExA(path) printf(1, path &" Total: %s\nUsed: %s\nAvailable: %s\n" , {comafy(sDiskSpace[TOTALNUMBEROFBYTES]) , comafy( sDiskSpace[TOTALNUMBEROFBYTES] - sDiskSpace[TOTALNUMBEROFFREEBYTES] ) , comafy(sDiskSpace[FREEBYTESAVAILABLETOCALLER]) }) void = wait_key()
12. Re: Directory info?
- Posted by DonCCole Sep 21, 2008
- 1017 views
I thought of that but how would retrieve the data? Say into a .dat file.
In your program do
system( "dir > myfile.txt", 2 )
Thanks Euphoric this is what I was looking for but didn't know how to do.
13. Re: Directory info?
- Posted by DerekParnell (admin) Sep 21, 2008
- 1045 views
GetDiskFreeSpace has a problem with disks bigger than 2gig,
That problem only occurs on ancient Windows systems (Win 95/98/ME) which are also unlikely to have large drives.
aFreeBytesAvailableToCaller = allocate(4) aTotalNumberOfBytes = allocate(4) aTotalNumberOfFreeBytes = allocate(4)
Should be
aFreeBytesAvailableToCaller = allocate(8) aTotalNumberOfBytes = allocate(8) aTotalNumberOfFreeBytes = allocate(8)
14. Re: Directory info?
- Posted by ne1uno Sep 21, 2008
- 1025 views
GetDiskFreeSpace has a problem with disks bigger than 2gig,
That problem only occurs on ancient Windows systems (Win 95/98/ME) which are also unlikely to have large drives.
Should be
aFreeBytesAvailableToCaller = allocate(8) aTotalNumberOfBytes = allocate(8) aTotalNumberOfFreeBytes = allocate(8)
good catch, I knew it was pointing to 8 bytes but wrongly assumed it was 8 bytes windows maintains.
15. Re: Directory info?
- Posted by otterdad Sep 22, 2008
- 1071 views
i use a similar version of that code posted by Al Getz in 03
16. Re: Directory info?
- Posted by ChrisB (moderator) Sep 22, 2008
- 1123 views
Again:
Do I have permission to include this into the v4.0 standard library? And if so, if you want acknowledgement, what name for you should we use in the docs?
Hi
So this would be exclusive to Windows? Would there be an equivalent Linux feature, or would Linux users have to rely on df, and would Linux programs error out if they tried to call this function?
Chris
17. Re: Directory info?
- Posted by just_me Sep 23, 2008
- 1171 views
Do I have permission to include this into the v4.0 standard library? And if so, if you want acknowledgement, what name for you should we use in the docs?
Sure. No acknowledgement required.
JG
18. Re: Directory info?
- Posted by just_me Sep 23, 2008
- 1078 views
GetDiskFreeSpace has a problem with disks bigger than 2gig, I modified your program to use GetDiskFreeSpaceEx
not really tested that well and output needs work to divide by 1024 or 1000 to get Gig's or Megs
True but I think that GetDiskFreeSpaceEx does not work for Win9* so GetDiskFreeSpace wuld be a better option.
Also, GetDiskFreeSpace provides useful information (sectors, clusters) that ...Ex does not.
JG