Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for existing files

Status
Not open for further replies.

YJeepster

Mechanical
Jul 18, 2002
21
0
0
CA
I am new to VB and was wondering how to go about checking to see if the file name the program has created already exists. I could use the commondialog1.showsave , Flag 2, but I don't want to see the dialog box. I just want the program to ask me if I want to overwrite the existing file.

Any clues?

Thanks

Geoff
 
Replies continue below

Recommended for you

You can use the Dir() function.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Public Function FileExists%(filename$, showErr As Boolean)
' Description
' Checks 'filename$' to find whether the filename given
' exists.
' Parameters
' Name Type Value
' -------------------------------------------------------------
' filename$ String The filename to be checked
'
' Returns
' True if the file exists
' False if the file does not exist
'
Dim f%
' Trap any errors that may occur
On Error Resume Next
' Get a free file handle to avoid using a file handle already in use
f% = FreeFile
' Open the file for reading
Open filename$ For Input As #f%
' Close it
Close #f%
' If there was an error, Err will be <> 0. In that case, we return False
FileExists% = Not (Err <> 0) Or Err = 70
If Not FileExists% And showErr Then MsgBox (Err.Description)
Err = 0
End Function
 
This is the Object Oriented way of doing what you request, and is now the preferred method using the VB file object:

If the function returns True then the file was created, if it returns false then the file already existed or an error occurred.

You must put a reference in to Microsoft Scripting Runtime, do this by selecting Project on the menu, click on References, find then tick Microsoft Scripting Runtime.

Private Function CreateFile(sFileName As String) As Boolean
On Error GoTo err_cmdCreateFile_Click
Dim oFileSystemObject As FileSystemObject
Dim oFolder As Folder
Dim oStream As TextStream
'You use the TextStream object to read and write
'to the file, experiment with it.

If sFileName = &quot;&quot; Then
sFileName = InputBox (&quot;Enter a file name.&quot;, _
&quot;Missing File Name&quot;)

End If

Set oFileSystemObject = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'Check that the destination folder exists
'Replace Temp with any filepath name
If oFileSystemObject.FolderExists(&quot;C:\temp&quot;) Then
Set oFolder = oFileSystemObject.GetFolder(&quot;C:\Temp&quot;)

Else
Set oFolder = oFileSystemObject.CreateFolder(&quot;C:\Temp&quot;)

End If

'Check does file exist
If oFileSystemObject.FileExists(&quot;C:\Temp\&quot; & sFileName) Then
MsgBox &quot;File already exists.&quot;, vbOKOnly
CreateFile = False

Else
Set oStream = oFileSystemObject.CreateTextFile _
(&quot;C:\Temp\&quot; & sFileName, True, True)
CreateFile = True

End If


Set oStream = oFileSystemObject.CreateTextFile(&quot;C:\Temp\&quot; _
& sFileName, True, True)

exit_CreateFile:
Set oFileSystemObject = Nothing

Exit Sub

err_CreateFile:
MsgBox &quot;Error number &quot; & Err.Number _
& Err.Description

CreateFile = False
GoTo exit_CreateFile

End Sub


Any problems let me know!

Derrick
 
Thanks for all your pervious help, I now have a revised idea of what exactly what I need. It is as follows:

I have a program that creates a file name based on give parameters. The file name format is as follows:

Drive:\Main Directory\***\***1234?.ext

*** = 3 letter code

? = a single letter

Now what I need my code to do is search the *** sub-directory for the new file name minus the last letter ‘?’
If it finds a match(s) I want it to list it(them) in a window and ask the user if they want to delete the old file(s).

I also need the code to let the user know if the exact file name already exists and to ask if they want to overwrite or cancel.

Any help will be great.

Thanks

Geoff
 
Use the previous code I gave you but manipulate the filename to achieve the result you require, it's fairly simple to do and should need no further explanation.
 
Status
Not open for further replies.
Back
Top