msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

searching a file

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
Ken



Joined: 04 Oct 2007
Posts: 7

PostPosted: Fri Jun 22, 2007 9:13 am    Post subject: searching a file Reply with quote

Hi All,

I want to search a file in some specified directory. The result i am
expecting is whether the file exists in that particular directory or not.

Example:
if i give search "c:\x.bin", the result should say whether file exists or not.

I want to use this in an application. If the file does not exists then i
need to create a file with some private data in it.

Is there any function in File systems to achieve this? Like we have
function to create a file and delete a file. Likewise do we have any
functions to search a file?


Thanks

Archived from group: microsoft>public>vb>syntax
Back to top
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Fri Jun 22, 2007 1:38 pm    Post subject: Re: searching a file Reply with quote

> I want to search a file in some specified directory. The result i am
> expecting is whether the file exists in that particular directory or not.
>
> Example:
> if i give search "c:\x.bin", the result should say whether file exists or
> not.
>
> I want to use this in an application. If the file does not exists then i
> need to create a file with some private data in it.
>
> Is there any function in File systems to achieve this? Like we have
> function to create a file and delete a file. Likewise do we have any
> functions to search a file?

Give this function a try...

Function FileExists(FileName As String) As Boolean
On Error GoTo ErrorHandler
' Make sure the file exists and is not a directory
If (GetAttr(FileName) And vbDirectory) vbDirectory Then
FileExists = True
End If
Exit Function
ErrorHandler:
FileExists = False
End Function


Rick
Back to top
View user's profile Send private message
MikeD



Joined: 04 Oct 2007
Posts: 3348

PostPosted: Fri Jun 22, 2007 1:49 pm    Post subject: Re: searching a file Reply with quote

"Ken" wrote in message @microsoft.com...
> Hi All,
>
> I want to search a file in some specified directory. The result i am
> expecting is whether the file exists in that particular directory or not.
>
> Example:
> if i give search "c:\x.bin", the result should say whether file exists or
> not.

That's not searching a file. That's not even searching FOR a file. That's
just determining if a file exists in a given directory.

Somebody will probably come along and tell you to use the Dir function.
Don't! It will cause problems if you're using Dir for anything else at the
time. There are plenty of other ways to determine if a file exists that
won't have consequences. Here's one:

(air code)

Public Function FileExists(ByVal sFileName As String) As Boolean

On Error GoTo EH

If (GetAttr(sFileName) And vbDirectory) vbDirectory Then
FileExists = True
End If

Exit Function

EH:

FileExists = False

End Function

--
Mike
Microsoft MVP Visual Basic

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group