473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read a file, knowing only a part of its name

I need to open a file for reading, but I only know part of it's name.

The file I want to read is in the format of xxx-yyyy-zzz.EXT

The last three digits of the file name are different on each pc, for example

PC7 has xxx-yyyy-zz1.ext
PC5 has xxx-yyyy-zz2.ext
PC3 has xxx-yyyy-zz8.ext

and so on.

I need to find out if that file exists by using the first 7 seven digits
and if it does exist, then open it up for reading.
The way I do this now is to loop through the folder, and look at the first
7seven digits using the substing method and if that name equals a constant
(xxx-yyyy) then I open that file.
It works, but it seems like extra work, plus depending on the number of
files it can eat up alot of time.
Any ideas


Feb 20 '06 #1
4 2783
Jason,

Retrieve just the file names that begin with the known string:

Dim dirs As String() = Directory.GetFiles("c:\test", "123-4567*")

Then see how many files you got and proceed accordingly:

If dirs.Length = 1 Then
'Display the file name
MsgBox(dirs(0))
ElseIf dirs.Length = 0 Then
MsgBox("Not found")
Else
MsgBox("More than 1 file found")
End If

Kerry Moorman
"Jason" wrote:
I need to open a file for reading, but I only know part of it's name.

The file I want to read is in the format of xxx-yyyy-zzz.EXT

The last three digits of the file name are different on each pc, for example

PC7 has xxx-yyyy-zz1.ext
PC5 has xxx-yyyy-zz2.ext
PC3 has xxx-yyyy-zz8.ext

and so on.

I need to find out if that file exists by using the first 7 seven digits
and if it does exist, then open it up for reading.
The way I do this now is to loop through the folder, and look at the first
7seven digits using the substing method and if that name equals a constant
(xxx-yyyy) then I open that file.
It works, but it seems like extra work, plus depending on the number of
files it can eat up alot of time.
Any ideas


Feb 20 '06 #2

"Jason" <ja***@someone.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
I need to open a file for reading, but I only know part of it's name.

The file I want to read is in the format of xxx-yyyy-zzz.EXT

The last three digits of the file name are different on each pc, for
example

PC7 has xxx-yyyy-zz1.ext
PC5 has xxx-yyyy-zz2.ext
PC3 has xxx-yyyy-zz8.ext


Use FindFirstFile with "xxx-yyyy-???.ext" or use DirectoryInfo

(FindFirstFile exported from Kernel32.dll)

http://forums.microsoft.com/MSDN/Sho...44544&SiteID=1
Feb 21 '06 #3
Kerry

That works great, but is there a way I can determine if that file first
exists?
CAn I do a ....

If ((Dim dirs As String() = Directory.GetFiles("c:\test", "123-4567*")) <>
true) Then
'File does not exist

or is there a easier way?

Thanks
"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
Jason,

Retrieve just the file names that begin with the known string:

Dim dirs As String() = Directory.GetFiles("c:\test", "123-4567*")

Then see how many files you got and proceed accordingly:

If dirs.Length = 1 Then
'Display the file name
MsgBox(dirs(0))
ElseIf dirs.Length = 0 Then
MsgBox("Not found")
Else
MsgBox("More than 1 file found")
End If

Kerry Moorman
"Jason" wrote:
I need to open a file for reading, but I only know part of it's name.

The file I want to read is in the format of xxx-yyyy-zzz.EXT

The last three digits of the file name are different on each pc, for
example

PC7 has xxx-yyyy-zz1.ext
PC5 has xxx-yyyy-zz2.ext
PC3 has xxx-yyyy-zz8.ext

and so on.

I need to find out if that file exists by using the first 7 seven digits
and if it does exist, then open it up for reading.
The way I do this now is to loop through the folder, and look at the
first
7seven digits using the substing method and if that name equals a
constant
(xxx-yyyy) then I open that file.
It works, but it seems like extra work, plus depending on the number of
files it can eat up alot of time.
Any ideas


Feb 22 '06 #4
Hi Jason,

Directory.GetFiles() returns a String array, not a boolean value, so you
can't do it that way.

In my opinion, this method will only return the names of matching files in
the folder, so they *have* to be existing, don't they ? Why would you need
to check if the file exists in this case ?

If you want to check if anything was returned at all, then just use Kerry's
method and check for the Length property of the returned String array.

Dim Dirs As String() = Directory.GetFiles("c:\test", "123-4567*")
If Dirs.Length > 0 then
'Atleast 1 file was found
Else
'No file matching this pattern was found
End if

"Jason" <ja***@someone.com> wrote in message
news:eh**************@TK2MSFTNGP15.phx.gbl...
Kerry

That works great, but is there a way I can determine if that file first
exists?
CAn I do a ....

If ((Dim dirs As String() = Directory.GetFiles("c:\test", "123-4567*")) <>
true) Then
'File does not exist

or is there a easier way?

Thanks
"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
Jason,

Retrieve just the file names that begin with the known string:

Dim dirs As String() = Directory.GetFiles("c:\test", "123-4567*")

Then see how many files you got and proceed accordingly:

If dirs.Length = 1 Then
'Display the file name
MsgBox(dirs(0))
ElseIf dirs.Length = 0 Then
MsgBox("Not found")
Else
MsgBox("More than 1 file found")
End If

Kerry Moorman
"Jason" wrote:
I need to open a file for reading, but I only know part of it's name.

The file I want to read is in the format of xxx-yyyy-zzz.EXT

The last three digits of the file name are different on each pc, for
example

PC7 has xxx-yyyy-zz1.ext
PC5 has xxx-yyyy-zz2.ext
PC3 has xxx-yyyy-zz8.ext

and so on.

I need to find out if that file exists by using the first 7 seven digits and if it does exist, then open it up for reading.
The way I do this now is to loop through the folder, and look at the
first
7seven digits using the substing method and if that name equals a
constant
(xxx-yyyy) then I open that file.
It works, but it seems like extra work, plus depending on the number of
files it can eat up alot of time.
Any ideas



Feb 22 '06 #5

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

Similar topics

6
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the...
16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
4
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
5
by: Sridhar | last post by:
Hi, I have created a project which contains classes to read the data from the database. This project has an App.Config file which contains the SqlConnection String. when this code is called from...
2
by: Neil | last post by:
Is there a way to read a backup file to see if it contains a file without restoring the backup file? We are missing a record from a table, and I need to find out when it was deleted. We have...
3
pradeepjain
by: pradeepjain | last post by:
<html> <head> </head> <body> <?php $filehandle = fopen("welcome.txt","r"); if(filesize("welcome.txt")=='0') {
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.