473,811 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

API FindFirstFile\F indNextFile vs GetFiles

Hi All,

I'm working on a program that requires searching multiple drives for multiple file types
and cataloging them based on certain geospatial attributes. All together, there are
hundreds of thousands of files on the drives. As part of the process, I'm currently using
the GetFiles method of the FileSystem object to retrieve collection of strings
representing a collection of a particular file type (for example, tif files). The problem
is that the GetFiles method doesn't seem to make any callbacks that would allow for me to
show some sort of meaningful progress, and the process can take a very long time. I know
I could fudge it with a marquee style progress bar and\or a busy mouse icon, but this
isn't ideal.

So, I was looking into using the API to do the grunt work. This would allow me to at
least display each filename in a label control as the search is being performed. I'm
trying to port Randy Birch's VB6 code available on his website at
http://vbnet.mvps.org/code/fileapi/r...l_multiple.htm . It's working
somewhat, except for the FindNextFile function which I've declared like so:

/////
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFi leA" _
(ByVal hFindFile As Int32, _
ByVal lpFindFileData As WIN32_FIND_DATA ) As IntPtr
/////

As currently implemented, the function is not moving the search to the next file. It is
always returning the first file in the directory and it returns all the properties of that
file correctly. And it returns this first file for as many times as there are files in
the directory. I'm thinking it's because the WIN32_FIND_DATA - which is a UDT in the VB6
code - is defined as an object in my code and Dim'd as

/////
Dim WFD As WIN32_FIND_DATA = New WIN32_FIND_DATA
/////

at the beginning of the SearchForFiles Sub. For reference, in my code WIN32_FIND_DATA
defined like so:

/////
<StructLayout(L ayoutKind.Seque ntial, _
CharSet:=CharSe t.Auto)_
Friend Class WIN32_FIND_DATA
Friend sfileAttributes As Int32 = 0
Friend creationTime_lo wDateTime As Int32 = 0
Friend creationTime_hi ghDateTime As Int32 = 0
Friend lastAccessTime_ lowDateTime As Int32 = 0
Friend lastAccessTime_ highDateTime As Int32 = 0
Friend lastWriteTime_l owDateTime As Int32 = 0
Friend lastWriteTime_h ighDateTime As Int32 = 0
Friend nFileSizeHigh As Int32 = 0
Friend nFileSizeLow As Int32 = 0
Friend dwReserved0 As Int32 = 0
Friend dwReserved1 As Int32 = 0
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=MAX_ PATH)_
Friend fileName As String = Nothing
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=14)_
Friend alternateFileNa me As String = Nothing
End Class
/////

How can I make it so the FindNextFile call actually moves on to the next file in the
directory?

Lance
Aug 31 '06
13 8575

Lance wrote:
Thanks Tom. Remember, WIN32_FIND_DATA as a class caused the "FatalExecution EngineError "
using your declarations, but as a structure, it was running (although the output of the
members didn't seem to jive).
Private Const MAX_PATH As Integer = 260

' if you using vb2005, you can acually use
System.Runtim.I nteropServices. ComTypes.FILETI ME
<StructLayout(L ayoutKind.Seque ntial)_
Private Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)_
Private Structure WIN32_FIND_DATA
Public dwFileAttribute s As Integer
Public ftCreationTime As FILETIME
Public ftLastAccessTim e As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Integer
Public nFileSizeLow As Integer
Public dwReserved0 As Integer
Public dwReserved1 As Integer

<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=MAX_ PATH)_
Public cFileName As String

<MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=14)_
Public cAlternate As String
End Structure

Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile
As IntPtr) As Boolean

Private Declare Auto Function FindFirstFile Lib "kernel32" ( _
ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA ) As IntPtr

Private Declare Function FindNextFile Lib "kernel32" ( _
ByVal hFindFile As IntPtr, _
ByRef lpFindFileData As WIN32_FIND_DATA ) As Boolean

This set of declares seems to work for me. Try them out and see what
you get...

--
Tom Shelton

Sep 1 '06 #11
Beautiful. That works perfectly. Thanks so much Tom!

Lance

"Tom Shelton" <to*@mtogden.co mwrote in message
news:11******** *************@7 4g2000cwt.googl egroups.com...
>
Lance wrote:
>Thanks Tom. Remember, WIN32_FIND_DATA as a class caused the "FatalExecution EngineError
"
using your declarations, but as a structure, it was running (although the output of the
members didn't seem to jive).

Private Const MAX_PATH As Integer = 260

' if you using vb2005, you can acually use
System.Runtim.I nteropServices. ComTypes.FILETI ME
<StructLayout(L ayoutKind.Seque ntial)_
Private Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)_
Private Structure WIN32_FIND_DATA
Public dwFileAttribute s As Integer
Public ftCreationTime As FILETIME
Public ftLastAccessTim e As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Integer
Public nFileSizeLow As Integer
Public dwReserved0 As Integer
Public dwReserved1 As Integer

<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=MAX_ PATH)_
Public cFileName As String

<MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=14)_
Public cAlternate As String
End Structure

Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile
As IntPtr) As Boolean

Private Declare Auto Function FindFirstFile Lib "kernel32" ( _
ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA ) As IntPtr

Private Declare Function FindNextFile Lib "kernel32" ( _
ByVal hFindFile As IntPtr, _
ByRef lpFindFileData As WIN32_FIND_DATA ) As Boolean

This set of declares seems to work for me. Try them out and see what
you get...

--
Tom Shelton

Sep 1 '06 #12
Herfried,

Thanks for that sample. I downloaded it last night and while it worked, I couldn't
initially wrap my head around it. It seems far, far, far different than any tutorial,
help file, and example that I have ever seen before. Because it seems to work so well, it
really makes me scratch my head as to why nothing like it is presented anyplace else.
They're always pushing the use of the GetFiles method. That's sort of frustrating. I
thought I was thinking outside the .Net box by going with the API, but this native .Net
method that you suggest is even further outside the box, IMO.

For now, I think I'm going to stick with the API method, as I can understand most of it.
But I'm going to play around with the code you linked to. Once I can figure out how to
debug it and then step through the code, I'm sure I will have learned a lot.

Thanks,
Lance

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:e2******** ******@TK2MSFTN GP02.phx.gbl...
"Lance" <nu***@business .comschrieb:
>I'm working on a program that requires searching multiple drives for multiple file
types and cataloging them based on certain geospatial attributes. All together, there
are hundreds of thousands of files on the drives. As part of the process, I'm
currently using the GetFiles method of the FileSystem object to retrieve collection of
strings representing a collection of a particular file type (for example, tif files).
The problem is that the GetFiles method doesn't seem to make any callbacks that would
allow for me to show some sort of meaningful progress, and the process can take a very
long time.

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnume rator.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 1 '06 #13

Herfried K. Wagner [MVP] wrote:
"Lance" <nu***@business .comschrieb:
I'm working on a program that requires searching multiple drives for
multiple file types and cataloging them based on certain geospatial
attributes. All together, there are hundreds of thousands of files on the
drives. As part of the process, I'm currently using the GetFiles method
of the FileSystem object to retrieve collection of strings representing a
collection of a particular file type (for example, tif files). The
problem is that the GetFiles method doesn't seem to make any callbacks
that would allow for me to show some sort of meaningful progress, and the
process can take a very long time.

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnume rator.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Herfried, that is a very nice (and clever) example of using asnyc
delegates. You are to be commended. But, it still doesn't really
solve the problem - simply because at the heart it still calls
GetFiles. And as much as I love GetFiles, it still returns all the
files in the directory in one chunk. Normally, this isn't an issue.
But, if you have a directory with thousands of files in it, then you
still have to wait for GetFiles to return until you can cancel your
operation, or present any progress, or do any real filtering.

The only way to really get "real time" progress that I know of is to
use the API calls, since you iterate one file at a time, not a whole
directory at a time...

--
Tom Shelton

Sep 1 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
20219
by: Demetri | last post by:
Using the GetFiles method of the DirectoryInfo instance one can pass in a search pattern of string type. For example: DirectoryInfo di = new DirectoryInfo("C:\temp"); FileInfo fi = di.GetFiles("*.doc"); That code would return all the files with the doc extension in the C:\temp folder. No problem.
3
30590
by: S. Han | last post by:
I'm using Directory.GetFiles to enumerate files in a directory. The problem is if you have to enumerate all files + subdirectories recursively, it takes too much memory, and it fails. Is there another way to enumerate files and subdirectories recursively which doesn't take too much memory in CS?
2
5981
by: John Smith | last post by:
Hello all: I am trying to search for more than one extension in a directory at the same time with the following code: string files = Directory.GetFiles(sDir, "*.doc*.dot"); However, this does not work nor does something like this: string files = Directory.GetFiles(sDir, "*.doc, *.dot");
3
26141
by: philin | last post by:
Hi: I am using GetFiles() method of directory class to get some file names. I want to do know how i can setup search pattern strings to get multiple file types. Like: GetFiles(Path,"*.Txt") just gets txt files. I want to get txt and doc files together. Thank a lot for your help. philin
11
12512
by: al jones | last post by:
I'm using filesystem.getfiles - and so far it's working correctly *however* I'd sure like to be able to pass it, as the last parameter, the extensions (plural) for which I'm looking. I assumed that "*.exe, *.dll" for example would work but it seems to not like that syntax. Would someone correct my misinformation and give me a way to pass more than a single pattern to that last parameter?? Thanks //al
1
1898
by: Hawk | last post by:
I was writing my own wildcard compare algorithm and making a custom GetFiles routine when I came across what I think is a bug in .net's own GetFiles. My routine was coming up with different results than GetFiles so naturally I thought it was my routine that was bugged but when I investigated it the results from my algorithm where exactly what I would expect. call: System.IO.Directory.GetFiles("d:\\windows\\system32\\", "*ae?*.*",...
1
2558
by: jobs | last post by:
Say I only have a single file I want to get information on. How can I adapt this code? Dim dr As DataRow Dim fi As FileInfo Dim dir As New DirectoryInfo(filename) Dim dt As New DataTable dt.Columns.Add("FileName", GetType(String)) dt.Columns.Add("CeateTime", GetType(String)) dt.Columns.Add("Length", GetType(String))
3
2553
by: Michael Jackson | last post by:
In my .NET 2.0 VS 2005 VB application, I'm using Directory.GetFiles(path) to get all the files in the directory. However, I'm getting an error regarding "Illegal character in Path", even though I can copy, etc the file using the Windows XP explorer. I can trap the error, but then this traps the entire GetFiles() function, not just the one bad file. Is there a way to just trap for the one bad file and continue on?
0
1372
by: tshad | last post by:
I am trying to do multiple Directory.GetFiles and append the results to one array that I will process. I tried this: string strFiles; strFiles = Directory.GetFiles(SemaSettings.InputFilePath, "GL*.*"); strFiles.CopyTo(Directory.GetFiles(SemaSettings.InputFilePath, "IN.*"),strFiles.Length-1); strFiles.CopyTo(Directory.GetFiles(SemaSettings.InputFilePath, "TX.*"),strFiles.Length-1,);
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10663
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.