Jag håller på med ett program som ska ge mig storleken på vissa filer i en katalog, <code> http://www.vb2themax.com/Item.asp?P...&Cat=140&ID=469 och http://www.vb2themax.com/Item.asp?P...CodeBank&ID=324 hjälpte mig att få katalogens storlek i bytes. Sen fick jag hjälp av Henkoz på chatten med följande kod:Storlek på vissa filer i en katalog
det börjar med att jag väljer vilken katalog med nedanstående kod:
<code>
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
'These constants are to be set to the ul
' Flags property in the BROWSEINFO type de
' pending of what result you want
Const BIF_RETURNONLYFSDIRS = &H1 'Allows you to browse for system folders only.
Const BIF_DONTGOBELOWDOMAIN = &H2 'Using this value forces the _
user to stay within the domain level of the _
Network Neighborhhood
Const BIF_STATUSTEXT = &H4 'Displays a statusbar on the selection dialog
Const BIF_RETURNFSANCESTORS = &H8 'Returns file system ancestor only
Const BIF_BROWSEFORCOMPUTER = &H1000 'Allows you to browse for a computer
Const BIF_BROWSEFORPRINTER = &H2000 'Allows you to browse the Printers folder
Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetFolder(Optional Title As String, Optional hWnd) As String
Dim bi As BROWSEINFO
Dim pidl As Long
Dim Folder As String
Folder = String$(255, Chr$(0))
With bi
If IsNumeric(hWnd) Then .hOwner = hWnd
.ulFlags = BIF_RETURNONLYFSDIRS
.pidlRoot = 0
If Not IsMissing(Title) Then
.lpszTitle = Title
Else
.lpszTitle = "Select a Folder" & Chr$(0)
End If
End With
pidl = SHBrowseForFolder(bi)
If SHGetPathFromIDList(ByVal pidl, ByVal Folder) Then
GetFolder = Left(Folder, InStr(Folder, Chr$(0)) - 1)
Else
GetFolder = ""
End If
End Function
</code>
Sen använder jag mig av flbFiles = GetFolder (flbFiles är en FileListBox), jag vill efter det få ut storleken på filerna som ligger i boxen, pattern är *.m2v,*.rar.
För att få ut en enstaka fils storlek använder jag mig av nedanstående.
<code>
Public Function FileSize(sFile As String) As String
'Function by Slikk - http://newbie.homelinux.net/index.php
Dim sSize As Single
If Len(Dir$(sFile$)) Then
Open sFile$ For Binary As #1
sSize = LOF(1)
Close #1
Select Case sSize
Case 0 To 1023
FileSize = sSize & " Bytes"
Case 1024 To 1048575
FileSize = Format(sSize / 1024#, "###0.00") & " KB"
Case 1024# ^ 2 To 1043741824
FileSize = Format(sSize / 1024# ^ 2, "###0") & " MB"
Case Is > 1043741824
FileSize = Format(sSize / 1024# ^ 3, "###0") & " GB"
End Select
End If
End Function
</code>
Förmodar att man ska använda sig av loopar men tyvärr så har jag aldrig förstått mig på dom. Tacksam för all hjälp jag kan få.Sv: Storlek på vissa filer i en katalog
Public Function FileSize(sFile As String) As long
'Function by Slikk - http://newbie.homelinux.net/index.php
Dim sSize As Single
If Len(Dir$(sFile$)) Then
Open sFile$ For Binary As #1
sSize = LOF(1)
Close #1
End If
End Function
private function FormatFileSize(Size as long) as string
Select Case Size
Case 0 To 1023
FormatFileSize = sSize & " Bytes"
Case 1024 To 1048575
FormatFileSize = Format(sSize / 1024#, "###0.00") & " KB"
Case 1024# ^ 2 To 1043741824
FormatFileSize = Format(sSize / 1024# ^ 2, "###0") & " MB"
Case Is > 1043741824
FormatFileSize = Format(sSize / 1024# ^ 3, "###0") & " GB"
End Select
end function
'Kod för att loopa
dim i as long
dim l as long
for i = 0 to flbfiles.count/length_eller_nå't - 1
l = l + FileSize(flbFiles.Item(i)) 'Eller nå't annat istället för item
next
dim s as string
s = FormatFileSize(l)
msgbox s
</code>
Det där borde fungera, då får modifiera på de två raderna strax ovanförSv: Storlek på vissa filer i en katalog
<code>
Dim lSize As Long
Dim sFileSize As String
lblCurrent.Caption = GetFolder
lstFiles.Path = lblCurrent.Caption
lSize = GetDirectorySize(lstFiles.Path)
Select Case lSize
Case 0 To 1024
sFileSize = lSize & " Bytes"
Case 1025 To 1048576
sFileSize = Format(lSize / 1024, "###0.00") & " KB"
Case 1048577 To 1073741824
sFileSize = Format(lSize / 1048576, "###0") & " MB"
Case Else
sFileSize = Format(lSize / 1073741824, "###0") & " GB"
End Select
txtFileSize = sFileSize
</code>