|
-
Oct 26th, 2004, 10:53 AM
#1
Thread Starter
Lively Member
If Statement (If File Exists)
could someone please have a look at this, there seems to be a problem, its not progressing pass the first ELSEIF.
I'm attempting to create a program that automatically installs DLL's & OCX files into the system32 folder.
VB Code:
Private Sub Label2_Click()
Dim FileToBeCopied As String
Dim Location As String
FileToBeCopied = Text1.Text
Location = "C:\WINDOWS\system32\"
If FileToBeCopied = "" Then
MsgBox "You Need To Select A File To Automatically Install", vbInformation, "No File Selected!"
ElseIf FileExists(Location & FileToBeCopied) Then
MsgBox "The File Has Been Detected As Already Existing", vbExclamation, "The Program Will End"
Else
On Error Resume Next 'Error Proof
FileCopy FileToBeCopied, Location 'Copy The File FILENAME, PATH
End If
End Sub
& this is what I have in the module for fileexists
VB Code:
Function FileExists(strFile As String) As Integer
Dim lSize As Long
On Error Resume Next
lSize = -1
lSize = FileLen(strFile)
If lSize = 0 Then
FileExists = 0
ElseIf lSize > 0 Then
FileExists = 1
Else
FileExists = -1
End If
End Function
any idea's...
Last edited by Shadows; Oct 27th, 2004 at 06:58 AM.
-
Oct 26th, 2004, 10:55 AM
#2
Explian "No progressing"
Woka
-
Oct 26th, 2004, 10:57 AM
#3
Thread Starter
Lively Member
well it's not progressing pass this
VB Code:
ElseIf FileExists(Location & FileToBeCopied) Then
MsgBox "The File Has Been Detected As Already Existing", vbExclamation, "The Program Will End"
I have manually checked to see that the file does NOT exist in the system folder however it is still displaying the MsgBox "The File Has Been Detected As Already Existing" and is NOT progressing to copy the file.
-
Oct 26th, 2004, 11:03 AM
#4
Try using
strRet = DIR(strFilePath)
to check for the existence of a file If strRet = "" then strFilePath does not exist
-
Oct 26th, 2004, 11:04 AM
#5
Thread Starter
Lively Member
lol, maybe progressing was thw wrong word, I mean its NOT getting PASSED that ElseIf
-
Oct 26th, 2004, 11:17 AM
#6
You are returning an integer and using it as a boolean. Take a look at what is being returned, and look at how it is being evaluated. You will probably see that it is evaluating in a way you didn't expect.
My usual boring signature: Nothing
 
-
Oct 26th, 2004, 12:06 PM
#7
Thread Starter
Lively Member
I'm sorry, I don't understand, its safe to say im currently learning VB and not familar with what your saying
-
Oct 26th, 2004, 01:22 PM
#8
Junior Member
There's a few problems.
VB Code:
ElseIf FileExists(Location & FileToBeCopied) Then
is combing both locations of the files (original and wanted location of the file to be copied). You also need to check the value of FileExists before continuing by doing something like this:
VB Code:
ElseIf FileExists(Location) = 1 Then
. This should get you started.
Last edited by Distortion; Oct 26th, 2004 at 01:42 PM.
-
Oct 26th, 2004, 01:52 PM
#9
The FileExists function has 3 possible return values 0, 1 -1. Your If statement is checking for a True value only. True is "any non-zero" value, so when FileExists returns 1 or -1, the If statement sees this as True and will execute the code.
Change your If statement to
ElseIf FileExists(Location & FileToBeCopied) = 1 Then
-
Oct 26th, 2004, 01:56 PM
#10
Junior Member
Yes, please ignore the beginning of my previous post. That was an error created by myself and not you.
-
Oct 27th, 2004, 06:58 AM
#11
Thread Starter
Lively Member
I managed to fix it using this method and it seems to be working perfectly.
VB Code:
If FileToBeCopied = "" Then
MsgBox "You Need To Select A File To Automatically Install", _
vbInformation, "No File Selected!" 'No File Selected
ElseIf Dir$("C:\WINDOWS\system32\" & FilenameTitle) <> "" Then
MsgBox "The File Has Been Detected As Already Existing", _
vbExclamation, "The Program Will End" 'File Detected
Else
On Error Resume Next 'Error Proof
FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
MsgBox "The File Was Successfully Copied", _
vbInformation, "Successful Copy" 'Display Message Box
End If
I put a textbox hidden in the form but not visible that when the user selects the filename from the common dialog, it shows the filetitle in that textbox so it checks
VB Code:
"C:\WINDOWS\system32\" & FilenameTitle
anyway thanks guys, learn something new everyday
-
Oct 27th, 2004, 07:04 AM
#12
VB Code:
On Error Resume Next 'Error Proof
FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
MsgBox "The File Was Successfully Copied", _
vbInformation, "Successful Copy" 'Display Message Box
Bad bad bad bad.
VB Code:
On Error Goto ErrorHandler
If FileToBeCopied = "" Then
MsgBox "You Need To Select A File To Automatically Install", _
vbInformation, "No File Selected!" 'No File Selected
ElseIf Dir$("C:\WINDOWS\system32\" & FilenameTitle) <> "" Then
MsgBox "The File Has Been Detected As Already Existing", _
vbExclamation, "The Program Will End" 'File Detected
Else
FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
MsgBox "The File Was Successfully Copied", _
vbInformation, "Successful Copy" 'Display Message Box
End If
Exit Sub
ErrorHandler:
MsgBox "ERROR!!! " & err.description
Also...
VB Code:
MsgBox "The File Was Successfully Copied", _
vbInformation, "Successful Copy" 'Display Message Box
Why span 2 lines???????
This is a pain the the butt to read...at 1st glance it looks like 2 lines...just do:
VB Code:
MsgBox "The File Was Successfully Copied", vbInformation, "Successful Copy" 'Display Message Box
Woka
-
Oct 27th, 2004, 07:08 AM
#13
Thread Starter
Lively Member
dunno, its just a habbit I picked up cause I dont like having to read code n scroll across the code window
thanks for the input, that errorhandler would be better, I've included that
Last edited by Shadows; Oct 27th, 2004 at 07:12 AM.
-
Oct 27th, 2004, 07:24 AM
#14
No probs.
Spanning multiple lines can cause "problems" when debugging...although the MsgBox isn't that much of a problem.
Lets say you have:
VB Code:
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM Jobs " _
& "WHERE Completed = 1 "
While debugging in runtime you realise you need to add "AND UserID = 3" as you missed this part off.
Then VB would reset your project for you to add this like:
VB Code:
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM Jobs " _
& "WHERE Completed = 1 " _
& "AND UserID = 3 "
If you did:
VB Code:
Dim strSQL As String
strSQL = "SELECT * "
strSQL = strSQL & "FROM Jobs "
strSQL = strSQL & "WHERE Completed = 1 "
Then when debugging you can change this SQL statement any which way, and the IDE will not reset your project. You could even delete the enire strSQL statement and VB would still not reset the project, where as when using _ it would.
Not a serious problem, just annoying.
Woof
-
Oct 27th, 2004, 07:53 AM
#15
Thread Starter
Lively Member
ok thanks man, Im currently learning VB so im targetting myself to learn 5 new things in VB a day to one day become a good programmer thne I can help people like you have helped me, I appreicate it
-
Oct 27th, 2004, 07:56 AM
#16
Err.Clear
After you've handled the error, otherwise another active error handler in the calling subs will pick up the error.
-
Oct 27th, 2004, 07:57 AM
#17
No probs.
Everyone starts somewhere 
If you ever need any help then give me a shout.
Woooooof
-
Oct 27th, 2004, 08:14 AM
#18
Thread Starter
Lively Member
Originally posted by leinad31
Err.Clear
After you've handled the error, otherwise another active error handler in the calling subs will pick up the error.
I didnt understand what you meant until I just tested the program, now I do, thank you
Originally posted by Wokawidget
No probs.
Everyone starts somewhere
If you ever need any help then give me a shout.
Woooooof
Appreicated
-
Oct 27th, 2004, 08:47 AM
#19
Don't you need to instantiate a fileSystemObject to check if a file or folder exists? Isn't that one of the methods of the object?
-
Oct 27th, 2004, 08:49 AM
#20
He used Dir() instead of a file system object.
In the same way as you can choose to use DAO or ADO to access a database.
Many ways to skin a cat.
-
Oct 27th, 2004, 08:55 AM
#21
and at the end of the day, the cat will still make the same noise when you skin it 
Woka
-
Oct 27th, 2004, 08:59 AM
#22
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
|