News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Help with files!

Started by LemonWizard, November 27, 2013, 11:19:54 PM

Previous topic - Next topic

LemonWizard

I need help with getting directory information.
The goal:

Create a program that can recursively scan each folder, get the size of each folder ( count the files in it, and their sizes, add the filesizes up for each folder), average the files in each folder (their size).
Then list this information.
Give a percentage remaining of total size ( as a goal 750 MB)

The average should be for each folder (Average file size of each folder) In a list.
I need help because I'm having issues using different loops to get playbasic to do this and it keeps reporting my file sizes as 0 except in one instance i got it to work but it wasn't very far along... I don't have the code for that anymore.
Here's the code I'm trying right now it's just freezing on me. I can't get it to continue..

PlayBASIC Code: [Select]
; PROJECT : Testing
; AUTHOR : Microsoft
; CREATED : 11/27/2013
; EDITED : 11/27/2013
; ---------------------------------------------------------------------
; Read and sort the Files/Folder names in the
; current directory
#include "Input"

dim folders$(25)
folders$(1)="C:\Sample Pack\Drums\Clap"
folders$(2)="Sample Pack\Drums\Kick"
folders$(3)="Sample Pack\Noise\128"
folders$(4)="Sample Pack\Noise\130"
folders$(5)="Sample Pack\Noise\Short"

dim folderfiles(10, 100)
dim averages(5)
//folderfiles x, 0 is the file in kb



for folders=1 to 5
; Set the current dir to the C drive

size=0
current=0
CD folders$(folders)


; Get the first file from the c drive
File$=FirstFile$( folders$(folders) )


; While File$ is not equal too nothing, then
; run the while loop
While File$<>""
; Display the File$ or folder name
if filetype(File$)=1
if file$<>"."
folderfiles(folders, current)=filesize(File$)
totalsize=totalsize+filesize(File$)
print File$




; Get the next file from this folder
File$=NextFile$()
current=current+1
endif
endif

sync
endwhile

; Just back to the while statement


; Display the screen and wait for key press

averages(t)=totalsize/current
next folders

print "average folder sizes in order of 1 to 5"
for t=1 to 5

print averages(t)
next t

sync
waitkey





  Mod Edit: please use the PBCODE tags rather than CODE tags.



kevin

  Here's a hint...

PlayBASIC Code: [Select]
   averages(t)=totalsize/current




   T isn't the loop counter in the first scanning loop, it's Folders


PlayBASIC Code: [Select]
 dim folders$(25)
folders$(1)="J:\_Mp3_Rips\_Music_From_8Gig_USBKEY\High School Musical\Vocals\"
folders$(2)="Sample Pack\Drums\Kick"
folders$(3)="Sample Pack\Noise\128"
folders$(4)="Sample Pack\Noise\130"
folders$(5)="Sample Pack\Noise\Short"

dim folderfiles(10, 100)
dim averages(5)
//folderfiles x, 0 is the file in kb


for folders=1 to 5
; Set the current dir to the C drive

current=0
TotalSize =0
CD folders$(folders)

; Get the first file from the c drive
File$=FirstFile$( folders$(folders) )

; While File$ is not equal too nothing, then
; run the while loop
While File$<>""
; Display the File$ or folder name
if filetype(File$)=1
if file$<>"." and file$<>".."
size=filesize(File$)
print File$+" "+Str$(Size)
folderfiles(folders, current)=size
totalsize+=size
current=current+1
endif
endif
File$=NextFile$()
endwhile

averages(Folders)=totalsize/current
next folders

print "average folder sizes in order of 1 to 5"
for t=1 to 5
print "folder #"+digits$(t,2)+" "+str$(averages(t))+" bytes"
next t

sync
waitkey






LemonWizard


LemonWizard

Thank you, but now the file sizes aren't acurate with this code.

PlayBASIC Code: [Select]
; PROJECT : new average sizes
; AUTHOR : Microsoft
; CREATED : 11/27/2013
; EDITED : 11/27/2013
; ---------------------------------------------------------------------

dim folders$(25)
folders$(1)="C:\Sample Pack\Drums\Clap"
folders$(2)="C:\Sample Pack\Drums\Kick"
folders$(3)="C:\Sample Pack\Noise\128"
folders$(4)="C:\Sample Pack\Noise\130"
folders$(5)="C:\Sample Pack\Noise\Short"
dim foldernames$(5)
foldernames$(1)="clap"
foldernames$(2)="Kick"
foldernames$(3)="128"
foldernames$(4)="130"
foldernames$(5)="short"


dim folderfiles(10, 100)
dim averages(5)
//folderfiles x, 0 is the file in kb


for folders=1 to 5
; Set the current dir to the C drive

current=0
TotalSize =0
CD folders$(folders)

; Get the first file from the c drive
File$=FirstFile$( folders$(folders) )

; While File$ is not equal too nothing, then
; run the while loop
While File$<>""
; Display the File$ or folder name
if filetype(File$)=1
if file$<>"." and file$<>".."
size=filesize(File$)
size=size/1024
print File$+" "+Str$(Size)+ " KB"
folderfiles(folders, current)=size
totalsize+=size
current=current+1
endif
endif
File$=NextFile$()
endwhile

averages(Folders)=totalsize/current
Y=GetCursorY()
if Y>getscreenheight()
setcursor folders*200, 0
endif

next folders
//cls
setcursor 200, 0
print "average folder sizes in order of 1 to 5"
for t=1 to 5
setcursor 200, t*12
print "folder #"+digits$(t,2)+" "+ foldernames$(t) + " " +str$(averages(t))+" KB"
next t

sync
waitkey








What am I doing wrong?
1024 Bytes=1 Kb

kevin

#4
  If you take the file length in bytes and divide that into K, then of course it won't be accurate.  The division is truncating the results, so if a file was say 350 bytes long, it'd be returned as (350/1024)=0 K  

 What you need to do is round them up to the nearest K, which is what the file system does.    


  edit:

PlayBASIC Code: [Select]
dim folders$(25)
folders$(1)="J:\DVD_PHOTOS"
folders$(2)="C:\Sample Pack\Drums\Kick"
folders$(3)="C:\Sample Pack\Noise\128"
folders$(4)="C:\Sample Pack\Noise\130"
folders$(5)="C:\Sample Pack\Noise\Short"
dim foldernames$(5)
foldernames$(1)="clap"
foldernames$(2)="Kick"
foldernames$(3)="128"
foldernames$(4)="130"
foldernames$(5)="short"


dim folderfiles(10, 100)
dim averages(5)
//folderfiles x, 0 is the file in kb


for folders=1 to 5
; Set the current dir to the C drive

current=0
TotalSize =0
CD folders$(folders)

; Get the first file from the c drive
File$=FirstFile$( folders$(folders) )

; While File$ is not equal too nothing, then
; run the while loop
While File$<>""
; Display the File$ or folder name
if filetype(File$)=1
if file$<>"." and file$<>".."
SizeInBytes=filesize(File$)

// divide size bye bytes per K
// add 1 to the K size when the modulus is bigger than zero.
Size =(sizeInBytes/1024) + (mod(SizeInBytes,1024)>0)


print File$+" "+Str$(Size)+ " KB"
folderfiles(folders, current)=size
totalsize+=size
current=current+1
endif
endif
File$=NextFile$()
endwhile

averages(Folders)=totalsize/current
Y=GetCursorY()
if Y>getscreenheight()
setcursor folders*200, 0
endif

next folders
Sync
waitkey
waitnokey



cls
setcursor 200, 0
print "average folder sizes in order of 1 to 5"
for t=1 to 5
setcursor 200, t*12
print "folder #"+digits$(t,2)+" "+ foldernames$(t) + " " +str$(averages(t))+" KB"
next t

sync
waitkey