|
-
Jun 17th, 2000, 04:35 AM
#1
Thread Starter
Hyperactive Member
I'm saving a listbox to a normal Text-File
with a common dialogue.
Now I want, that a message box appears when a
file(name) alredy exists. There, the user can decide,
if he wants to overwright the old file.
How do I program this message box in VB3 ???
Thanks for some help, Matt
-
Jun 17th, 2000, 05:40 PM
#2
Lively Member
Set this flag
I don't know if this will work in VB3, but give it a go... Set the following flag for the file save dialogue.
cdlOfNOOverwritePrompt
Code:
Private sub cmdSave_Click()
Dim strFilename as string
'common dialogue called cdl1
'Prompt the user if they select a file that already exists.
cdl1.Flags = cdlOfNOOverwritePrompt
cdl1.ShowSave
strFilename = cdl1.Filename
End sub
If you just want to check the existence of a file, use DIR as Batman mentioned.
Code:
'Function...
Public Function FileExists(ByVal strFilename as string) As Boolean
If Dir(strFilename, vbNormal) <> "" Then
FileExists = True
Else
FileExists = False
End If
End Function
'To use call the following.
Dim intRet as integer
If FileExists("C:\Text.txt") Then
intRet = Msgbox("Are you sure you want to overwrite the file " & strFilename & " ?", vbYesNO + vbdefaultButton2)
Select Case intRet
Case vbYes
'Overwrite file
Case Else
exit sub
End select
Else
'Save file, it doesn't al ready exist.
End IF
Hope this helps in some way.
Laterz
REM
-
Jun 18th, 2000, 02:16 PM
#3
New Member
Here is another method for checking file existence as I read somewhere that the GetAttr method is a little faster than the Dir function.
Public Function Exist(FileOrDirName As String) As Boolean
Dim FNum As Integer
Dim a As Integer
On Error Resume Next
Err.Clear
If InStr(1, FileOrDirName, "*") = 0 And InStr(1, DateiOderVerzName, "?") = 0 Then
a = GetAttr(FileOrDirName)
If Err.Number <> 0 Then
Exist = False
Err.Clear
Else
Exist = True
End If
Else
If Dir(FileOrDirName) <> "" Then Exist = True Else Exist = False
End If
End Function
Here 2 functions for separate check if a file or folder exists:
Public Function CheckFile(FileName As String) As Boolean
Dim a As Integer
On Error Resume Next
Err.Clear
a = GetAttr(FileName)
If Err.Number <> 0 Or ((a And vbDirectory) = vbDirectory) Then
Err.Clear
CheckFile = False
Else
CheckFile = True
End If
End Function
Public Function CheckDir(DirName As String) As Boolean
Dim a As Integer
On Error Resume Next
Err.Clear
a = GetAttr(DirName)
If Err.Number <> 0 Or ((a And vbDirectory) <> vbDirectory) Then
Err.Clear
CheckDir = False
Else
CheckDir = True
End If
End Function
Using VBS, Java, JavaScript
-
Jun 18th, 2000, 03:32 PM
#4
New Member
Use the FileLen function.
If it returns a error then the file does not exist.
-
Jun 20th, 2000, 06:31 AM
#5
New Member
If you OR the Dir arguments together, then one function can be used for all types of files or directories.
Code:
'Find out if a file (or directory) exists
Private Function Exists(ByVal sFileNameA As String) As Boolean
Dim bExists As Boolean
bExists = False
If Dir$(sFileNameA, vbNormal + vbHidden + vbSystem + vbVolume + _
vbDirectory) <> "" Then
bExists = True
End If
Exists = bExists
End Function
The most likely way for the world to be destroyed, most experts agree, is by
accident. That's where we come in; we're computer professionals. We cause
accidents.
- Nathaniel Borenstein
-
Jun 20th, 2000, 06:56 AM
#6
transcendental analytic
As a rule use OR operator instead of + when you add flags,
Code:
Fileexist=cbool(len(Dir$(FileName, vbNormal OR vbHidden OR vbSystem OR vbVolume OR vbDirectory)))
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|