Check For File

J

Jeanmcgru

Using Access ’03…

Doug Steele sent me some useful code to set a file attribute in VBA:

SetAttr "C:\Folder\File.mdb", GetAttr("C:\Folder\File.mdb") Or
vbHidden

I use the following code to copy a file from one location to another,
and then hide the file:

FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

This works fine, but it won’t let me do it twice and copy over the
preexisting file because the file is hidden.

I performed this workaround:

SetAttr "C:\Folder\File.mdb", vbNormal 'show file
FileCopy SourceFile, DestinationFile 'copy source file to destination
SetAttr "C:\Folder\File.mdb", vbHidden 'hide file

The code above works, but not the first time because the mdb does not
exist on the drive until the code runs at least once; i.e., I cannot
set an attribute on a file that does not exist.

I need to first check to see if the file exists, if so, then run the
first part of the code:

If File.mdb exists in C:\Folder Then
SetAttr "C:\Folder\File.mdb", vbNormal
Else
Nothing
End If
FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

Any ideas on how to check a location to see if a file exists?
alex
 
A

alex

Using Access ’03…

Doug Steele sent me some useful code to set a file attribute in VBA:

SetAttr "C:\Folder\File.mdb", GetAttr("C:\Folder\File.mdb") Or
vbHidden

I use the following code to copy a file from one location to another,
and then hide the file:

FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

This works fine, but it won’t let me do it twice and copy over the
preexisting file because the file is hidden.

I performed this workaround:

SetAttr "C:\Folder\File.mdb", vbNormal 'show file
FileCopy SourceFile, DestinationFile 'copy source file to destination
SetAttr "C:\Folder\File.mdb", vbHidden 'hide file

The code above works, but not the first time because the mdb does not
exist on the drive until the code runs at least once; i.e., I cannot
set an attribute on a file that does not exist.

I need to first check to see if the file exists, if so, then run the
first part of the code:

If File.mdb exists in C:\Folder Then
SetAttr "C:\Folder\File.mdb", vbNormal
Else
Nothing
End If
FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

Any ideas on how to check a location to see if a file exists?
alex
 
A

Arvin Meyer MVP

Try this:

Function FileExists (strFile As String) As Integer
On Error Resume Next
Dim intLen As Integer

intLen = Len(Dir(strFile))

FileExists = (Not Err And intLen > 0)

End Function
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Using Access ’03…

Doug Steele sent me some useful code to set a file attribute in VBA:

SetAttr "C:\Folder\File.mdb", GetAttr("C:\Folder\File.mdb") Or
vbHidden

I use the following code to copy a file from one location to another,
and then hide the file:

FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

This works fine, but it won’t let me do it twice and copy over the
preexisting file because the file is hidden.

I performed this workaround:

SetAttr "C:\Folder\File.mdb", vbNormal 'show file
FileCopy SourceFile, DestinationFile 'copy source file to destination
SetAttr "C:\Folder\File.mdb", vbHidden 'hide file

The code above works, but not the first time because the mdb does not
exist on the drive until the code runs at least once; i.e., I cannot
set an attribute on a file that does not exist.

I need to first check to see if the file exists, if so, then run the
first part of the code:

If File.mdb exists in C:\Folder Then
SetAttr "C:\Folder\File.mdb", vbNormal
Else
Nothing
End If
FileCopy SourceFile, DestinationFile
SetAttr "C:\Folder\File.mdb", vbHidden

Any ideas on how to check a location to see if a file exists?
alex
 
D

Douglas J. Steele

If Len(Dir("C:\Folder\File.mdb")) > 0 Then
' File exists
Else
' File doesn't exist
End If
 
A

alex

If Len(Dir("C:\Folder\File.mdb")) > 0 Then
' File exists
Else
' File doesn't exist
End If

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no private e-mails, please)



















- Show quoted text -

Thanks guys for the help! Much appreciated.
alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top