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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the size of a Folder in KBytes

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 Dir2.path, how can I measure if the target drive has enough hard disk space to store those files ?
 
Oostwijk I see that you have not voted ANY of your replies as helpfull. To get the most from this site, Please read FAQ222-2244 (Item 15).

Free Space on selected drive..

Option Explicit

'API Declarations
Private Declare Function GetDiskFreeSpace Lib "kernel32.dll" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long

'Call this function to get the Amount of Free Disk Space (in Bytes)
'Pass to this function only the letter of the drive
Function FreeDiskSpace(DriveLetter As String)
Dim SectorsPerCluster As Long
Dim BytesPerSector As Long
Dim FreeClusters As Long
Dim NumberOfClusters As Long
Dim ret As Long

ret = GetDiskFreeSpace(DriveLetter & ":\", SectorsPerCluster, BytesPerSector, FreeClusters, NumberOfClusters)
FreeDiskSpace = BytesPerSector * SectorsPerCluster * FreeClusters
End Function

Private Sub Command1_Click()
MsgBox FreeDiskSpace("C") \ 1024 & " Kb Free Space in Drive C"
End Sub

Good luck! :)

 
This is one way to copy files from one dir to another..

Public Function CopyDir(Source As String, Dest As String, OverWrite As Boolean)
'This function uses the Windows Scripting Host
'You must have the scripting engine installed to use this code.
Dim fso
'Create file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'put windows scripting host to work
fso.CopyFolder Source, Dest, OverWrite
End
 
'Oostwijk I see that you have not voted ANY of your replies as helpfull. To get the most from this site, Please read FAQ222-2244 (Item 15).'

I'm sorry Lplates, I wasn't aware of that.

Is it also possible to calculate the total of files in filelistbox1 in kb ?

 
You could loop through the DirListBox, calling the GetFileSize API for each, and keeping a running total.

Details of GetFileSize here:
which includes a link to a VB example

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
You can use the API as LPlates and johnwm suggested or use FileSystemObject.

' Add a reference to Microsoft Scripting Runtime

Dim fso As New FileSystemObject
Dim drv As Drive
Dim fld As Folder
Dim TotalFreeSpace As String

Private Sub Command1_Click()
Set drv = fso.Drives("S:\")
TotalFreeSpace = FormatNumber(drv.TotalSize / 1024, 0) & " KB"
MsgBox TotalFreeSpace

Set fld = fso.GetFolder("S:\17916sd\")
TotalFreeSpace = FormatNumber(fld.Size / 1024, 0) & " KB"
MsgBox TotalFreeSpace
End Sub

Swi
 

So what happened to the use of FileLen(PathToFile)???

Good Luck

 
FileLen or LOF (When the file is opened) gives an error on files over 2 gig in size.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top