Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drive space

Status
Not open for further replies.

Oostwijk

Technical User
Oct 19, 2003
82
NL
I want to know if I can copy the files in filelistbox1 to the target defined in Dir1.path, how can I measure if the target drive has enough hard disk space to store those files ?
 
This procedure should do what you want:

Private Function DriveFreeSpace(DrivePath)
Dim fso, drv
Set fso = CreateObject("Scripting.FileSystemObject")
Set drv = fso.GetDrive(fso.GetDriveName(DrivePath))
DriveFreeKBytes = FormatNumber(drv.FreeSpace / 1024, 0)
End Function

Drive Path is the drive's path (e.g., "C:\"). Note that the free space is returned in kilobytes. To get megabytes, divide by 1024000: FormatNumber(drv.FreeSpace / 1024000, 0)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Ok, that works. But how to retrieve the size of the files in filelistbox1 ?
 
This will give the total size of all files in any directory supplied to the function:

Private Function FileSize(ByVal FolderPath As String) As Long
Dim fso, fldr, fc, f

Set fso = CreateObject("Scripting.FileSystemObject")

Set fldr = fso.GetFolder(FolderPath & "\")

Set fc = fldr.Files

For Each f In fc
FileSize = FileSize + f.Size
Next

FileSize = FileSize / 1024
End Function

For a FileListBox named "File1", just call the function like this:

x = FileSize(File1.Path)

also, there were a couple of errors in the first function. Use this:

Private Function DriveFreeSpace(DrivePath) As Long
Dim fso, drv
Set fso = CreateObject("Scripting.FileSystemObject")
Set drv = fso.GetDrive(fso.GetDriveName(DrivePath))
DriveFreeSpace = drv.FreeSpace / 1024
End Function

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
I have an application in Windows 98SE PCs that encodes live TV in WMV format. I need to control every minute the free space on HD, because generates large files.
I used the function you suggest and works fine in Windows XP, but in W98SE shows an incorrect number which never changes.

Please tell me if there's some consideration in W98 with that function.

Vladimir
 
The FreeSpace method is only good for values up to 2gb.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top