ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   File Exists (https://www.excelbanter.com/excel-programming/381847-file-exists.html)

[email protected]

File Exists
 
Using 2003 - I need to know if a file exists in a selected folder.
When I looked up the FileExists function in the online help I found the
following:
Description

Returns True if a specified file exists; False if it does not.

Syntax

object.FileExists(filespec)

The FileExists method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file whose existence is to be
determined. A complete path specification (either absolute or relative)
must be provided if the file isn't expected to exist in the current
folder.

That doesn't tell me anything I can understand. There isn't any
example.

Let's say the file I need to know about is "ABC.txt" and the folder I
will query is C:\MyFolder. What is the code I need to write that will
tell me if the file exists or not?

Glen


JLatham

File Exists
 
Glen,
See if this example doesn't clear things up some (yes, I agree, sometimes
the answer they give doesn't seem like an answer unless you already know the
answer).

Sub FSO_Test()
Dim myFSO As Object
Dim myResult As Boolean

Set myFSO = CreateObject("Scripting.FileSystemObject")
myResult = myFSO.FileExists("C:\ShowMeWhere.txt")
If myResult Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub

You could use a variable name in place of the path if you need to build up
the path and filename that you are looking for.

If you don't need to save the results of the test you can even shorten it to:

Sub FSO_Test()
Dim myFSO As Object

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FileExists("C:\ShowMeWhere.txt") Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub


" wrote:

Using 2003 - I need to know if a file exists in a selected folder.
When I looked up the FileExists function in the online help I found the
following:
Description

Returns True if a specified file exists; False if it does not.

Syntax

object.FileExists(filespec)

The FileExists method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file whose existence is to be
determined. A complete path specification (either absolute or relative)
must be provided if the file isn't expected to exist in the current
folder.

That doesn't tell me anything I can understand. There isn't any
example.

Let's say the file I need to know about is "ABC.txt" and the folder I
will query is C:\MyFolder. What is the code I need to write that will
tell me if the file exists or not?

Glen



Henry Jones[_2_]

File Exists
 
Why would you use the "CreateObject" and the FileExists when you can just
use the

If Dir("C:\MyFolder\ABC.txt") < "" Then

Isn't it much easier and less code?


"JLatham" <HelpFrom @ Jlathamsite.com.(removethis) wrote in message
...
Glen,
See if this example doesn't clear things up some (yes, I agree, sometimes
the answer they give doesn't seem like an answer unless you already know
the
answer).

Sub FSO_Test()
Dim myFSO As Object
Dim myResult As Boolean

Set myFSO = CreateObject("Scripting.FileSystemObject")
myResult = myFSO.FileExists("C:\ShowMeWhere.txt")
If myResult Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub

You could use a variable name in place of the path if you need to build up
the path and filename that you are looking for.

If you don't need to save the results of the test you can even shorten it
to:

Sub FSO_Test()
Dim myFSO As Object

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FileExists("C:\ShowMeWhere.txt") Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub


" wrote:

Using 2003 - I need to know if a file exists in a selected folder.
When I looked up the FileExists function in the online help I found the
following:
Description

Returns True if a specified file exists; False if it does not.

Syntax

object.FileExists(filespec)

The FileExists method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file whose existence is to be
determined. A complete path specification (either absolute or relative)
must be provided if the file isn't expected to exist in the current
folder.

That doesn't tell me anything I can understand. There isn't any
example.

Let's say the file I need to know about is "ABC.txt" and the folder I
will query is C:\MyFolder. What is the code I need to write that will
tell me if the file exists or not?

Glen





Dave Peterson

File Exists
 
I use a variation of dir() when I want to check:

dim TestStr as string
teststr = ""
on error resume next
teststr = dir("C:\myfolder\abc.txt")
on error goto 0

if teststr = "" then
....

I found that when I give it a file on a drive that I can't access (or doesn't
exist), this code doesn't cause an error.

With the FSO code, you don't need to be so careful.


Henry Jones wrote:

Why would you use the "CreateObject" and the FileExists when you can just
use the

If Dir("C:\MyFolder\ABC.txt") < "" Then

Isn't it much easier and less code?

"JLatham" <HelpFrom @ Jlathamsite.com.(removethis) wrote in message
...
Glen,
See if this example doesn't clear things up some (yes, I agree, sometimes
the answer they give doesn't seem like an answer unless you already know
the
answer).

Sub FSO_Test()
Dim myFSO As Object
Dim myResult As Boolean

Set myFSO = CreateObject("Scripting.FileSystemObject")
myResult = myFSO.FileExists("C:\ShowMeWhere.txt")
If myResult Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub

You could use a variable name in place of the path if you need to build up
the path and filename that you are looking for.

If you don't need to save the results of the test you can even shorten it
to:

Sub FSO_Test()
Dim myFSO As Object

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FileExists("C:\ShowMeWhere.txt") Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub


" wrote:

Using 2003 - I need to know if a file exists in a selected folder.
When I looked up the FileExists function in the online help I found the
following:
Description

Returns True if a specified file exists; False if it does not.

Syntax

object.FileExists(filespec)

The FileExists method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file whose existence is to be
determined. A complete path specification (either absolute or relative)
must be provided if the file isn't expected to exist in the current
folder.

That doesn't tell me anything I can understand. There isn't any
example.

Let's say the file I need to know about is "ABC.txt" and the folder I
will query is C:\MyFolder. What is the code I need to write that will
tell me if the file exists or not?

Glen



--

Dave Peterson

JLatham

File Exists
 
I agree 100% - DIR would be just as effective, generally. However, I don't
know how far Glen has gotten into all of this, plus his question directly
addressed the FSO.

Quite frankly, I'm rather old-fashioned in my coding of such things also,
and would have probably offered DIR() as the way to go had his question been
simply "how can I tell if a file exists in a folder or not". Then someone
would have offered up the FSO, making me look quite old-fashioned <g

Your post and Dave Peterson's now offer him a choice - while mine at least
offered him a learning experience?

"Henry Jones" wrote:

Why would you use the "CreateObject" and the FileExists when you can just
use the

If Dir("C:\MyFolder\ABC.txt") < "" Then

Isn't it much easier and less code?


"JLatham" <HelpFrom @ Jlathamsite.com.(removethis) wrote in message
...
Glen,
See if this example doesn't clear things up some (yes, I agree, sometimes
the answer they give doesn't seem like an answer unless you already know
the
answer).

Sub FSO_Test()
Dim myFSO As Object
Dim myResult As Boolean

Set myFSO = CreateObject("Scripting.FileSystemObject")
myResult = myFSO.FileExists("C:\ShowMeWhere.txt")
If myResult Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub

You could use a variable name in place of the path if you need to build up
the path and filename that you are looking for.

If you don't need to save the results of the test you can even shorten it
to:

Sub FSO_Test()
Dim myFSO As Object

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FileExists("C:\ShowMeWhere.txt") Then
MsgBox "File Exists"
Else
MsgBox "File Does Not Exist"
End If
Set myFSO = Nothing
End Sub


" wrote:

Using 2003 - I need to know if a file exists in a selected folder.
When I looked up the FileExists function in the online help I found the
following:
Description

Returns True if a specified file exists; False if it does not.

Syntax

object.FileExists(filespec)

The FileExists method syntax has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filespec Required. The name of the file whose existence is to be
determined. A complete path specification (either absolute or relative)
must be provided if the file isn't expected to exist in the current
folder.

That doesn't tell me anything I can understand. There isn't any
example.

Let's say the file I need to know about is "ABC.txt" and the folder I
will query is C:\MyFolder. What is the code I need to write that will
tell me if the file exists or not?

Glen







All times are GMT +1. The time now is 04:53 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com