|
-
Jun 5th, 2010, 01:04 PM
#1
Thread Starter
Hyperactive Member
Suggestion Required on getting Multiple files size...
Hello coders..
As part of my task, I need to get file size of some files, which i can get very easily by using FileInfo class.. As of now, I'm looping through all files in My Directory and then taking their file sized into a notepad.
But the real problem for me is,Like this, I'm having around 100 Root directories, each contains ~ 1000 subdirectories and each sub directory contains ~ 80 files..
Yes, I know, that, I can use a recursive function and loop through all dirs and sub dirs and thus the files.. which was taking huge efforts in terms of time.
I want to know, If there was any other alternative, which will make my task more easier and efficient..
If it is clear, Then i would like to further ask some more questions on this topic.
The Difference between a Successful person and others is not a Lack of Knowledge,
But rather a Lack of WILL 
-
Jun 5th, 2010, 06:44 PM
#2
Re: Suggestion Required on getting Multiple files size...
You can use a backgroundworker and let it run.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 5th, 2010, 10:48 PM
#3
Thread Starter
Hyperactive Member
-
Jun 5th, 2010, 11:49 PM
#4
Re: Suggestion Required on getting Multiple files size...
Open Windows Explorer and then right-click on a large folder and select Properties. Notice how the size of the folder is calculated before your eyes? If Windows Explorer has to do it then your app has to do it too.
I've never used it so I can't give you details but, with regards to memory usage, you should look at the Process class and, in particular, its MinWorkingSet property.
-
Jun 6th, 2010, 11:39 PM
#5
Re: Suggestion Required on getting Multiple files size...
Hi.
The only other method i'm aware and it's worth taking a look at for speed is to use API. Supposedly explorer uses API for getting the files so you may want to try this.XP tested.
Put a button (button2) and a textbox(textbox1).
Code:
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Dim hsearch As IntPtr
Dim findinfo As WIN32_FIND_DATA
Dim retv As Integer
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function FindFirstFile(ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Public Shared Function FindClose(ByVal hFindFile As IntPtr) As Boolean
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Structure WIN32_FIND_DATA
Public dwFileAttributes As Int32
Public ftCreationTime As System.Runtime.InteropServices.FILETIME
Public ftLastAccessTime As System.Runtime.InteropServices.FILETIME
Public ftLastWriteTime As System.Runtime.InteropServices.FILETIME
Public nFileSizeHigh As Int32
Public nFileSizeLow As Int64
Public dwReserved0 As Int32
Public dwReserved1 As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public cFileName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> Public cAlternateFileName As String
End Structure
'''' Put all the above before the form generated code,if you have it in the '''same file
'After...
Private intfilesize As Int64 = 0
Public Function goread(ByVal strfilename As String) As Integer
Try
hsearch = FindFirstFile(strfilename, findinfo)
intfilesize = intfilesize + findinfo.nFileSizeLow
' Close the file search handle
retv = FindClose(hsearch)
Catch ex As IOException
MsgBox(ex.ToString)
End Try
End Function
Public Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory.
Dim fileName As String
For Each fileName In fileEntries
goread(fileName)
Next fileName
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
' Recurse into subdirectories of this directory.
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub
''Here is the button click.Try whatever directory you want (it will search from
' it's room and go for all the above directories.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ProcessDirectory("c:\MyRootDirectory")
TextBox1.Text = intfilesize.ToString
End Sub
I don't know if you can,must,will do conversions for int64,integer etc but the actual total file size number i tested is correct.
P.S. This is completely out of the blue API.I haven't found any solid code (for the goread function) so you must test and test and test.
P.S.2 Win7 have some files and dirs that cannot be accessed.I don't know what will happen but i suppose you can write something to skip them.
P.S.3. I lied( ) There is another method that you pass a streamreader.BaseStream.Length of a file.This method is known and is extremely slower than the other methods so i won't bother to post it.
P.S.4. I should put this in my signature for anyone interested...
Last edited by sapator; Jun 6th, 2010 at 11:44 PM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|