Checking for existing files
Checking for existing files
(OP)
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
Any clues?
Thanks
Geoff
RE: Checking for existing files
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: Checking for existing files
' 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
RE: Checking for existing files
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 = "" Then
sFileName = InputBox ("Enter a file name.", _
"Missing File Name")
End If
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")
'Check that the destination folder exists
'Replace Temp with any filepath name
If oFileSystemObject.FolderExists("C:\temp") Then
Set oFolder = oFileSystemObject.GetFolder("C:\Temp")
Else
Set oFolder = oFileSystemObject.CreateFolder("C:\Temp")
End If
'Check does file exist
If oFileSystemObject.FileExists("C:\Temp\" & sFileName) Then
MsgBox "File already exists.", vbOKOnly
CreateFile = False
Else
Set oStream = oFileSystemObject.CreateTextFile _
("C:\Temp\" & sFileName, True, True)
CreateFile = True
End If
Set oStream = oFileSystemObject.CreateTextFile("C:\Temp\" _
& sFileName, True, True)
exit_CreateFile:
Set oFileSystemObject = Nothing
Exit Sub
err_CreateFile:
MsgBox "Error number " & Err.Number _
& Err.Description
CreateFile = False
GoTo exit_CreateFile
End Sub
Any problems let me know!
Derrick
RE: Checking for existing files
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
RE: Checking for existing files
RE: Checking for existing files
dongxiao PEng
http://www.cadtool.net