Results 1 to 22 of 22

Thread: If Statement (If File Exists)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119

    Resolved 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:
    1. Private Sub Label2_Click()
    2.  
    3.     Dim FileToBeCopied As String
    4.     Dim Location As String
    5.    
    6.     FileToBeCopied = Text1.Text
    7.     Location = "C:\WINDOWS\system32\"
    8.    
    9.     If FileToBeCopied = "" Then
    10.    
    11.         MsgBox "You Need To Select A File To Automatically Install", vbInformation, "No File Selected!"
    12.        
    13.     ElseIf FileExists(Location & FileToBeCopied) Then
    14.    
    15.         MsgBox "The File Has Been Detected As Already Existing", vbExclamation, "The Program Will End"
    16.        
    17.     Else
    18.    
    19.         On Error Resume Next 'Error Proof
    20.         FileCopy FileToBeCopied, Location 'Copy The File FILENAME, PATH
    21.        
    22.     End If
    23.            
    24. End Sub

    & this is what I have in the module for fileexists

    VB Code:
    1. Function FileExists(strFile As String) As Integer
    2.  
    3.     Dim lSize As Long
    4.  
    5.     On Error Resume Next
    6.  
    7.     lSize = -1
    8.  
    9.     lSize = FileLen(strFile)
    10.  
    11.     If lSize = 0 Then
    12.  
    13.         FileExists = 0
    14.  
    15.     ElseIf lSize > 0 Then
    16.  
    17.         FileExists = 1
    18.  
    19.     Else
    20.  
    21.         FileExists = -1
    22.  
    23.     End If
    24.  
    25. End Function

    any idea's...
    Last edited by Shadows; Oct 27th, 2004 at 06:58 AM.

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    well it's not progressing pass this

    VB Code:
    1. ElseIf FileExists(Location & FileToBeCopied) Then
    2.    
    3.         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.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Try using

    strRet = DIR(strFilePath)

    to check for the existence of a file If strRet = "" then strFilePath does not exist

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    lol, maybe progressing was thw wrong word, I mean its NOT getting PASSED that ElseIf

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    I'm sorry, I don't understand, its safe to say im currently learning VB and not familar with what your saying

  8. #8
    Junior Member
    Join Date
    Oct 2004
    Location
    South Carolina
    Posts
    19
    There's a few problems.
    VB Code:
    1. 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:
    1. ElseIf FileExists(Location) = 1 Then
    . This should get you started.
    Last edited by Distortion; Oct 26th, 2004 at 01:42 PM.

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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

  10. #10
    Junior Member
    Join Date
    Oct 2004
    Location
    South Carolina
    Posts
    19
    Yes, please ignore the beginning of my previous post. That was an error created by myself and not you.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    I managed to fix it using this method and it seems to be working perfectly.

    VB Code:
    1. If FileToBeCopied = "" Then
    2.    
    3.         MsgBox "You Need To Select A File To Automatically Install", _
    4.         vbInformation, "No File Selected!" 'No File Selected
    5.        
    6.     ElseIf Dir$("C:\WINDOWS\system32\" & FilenameTitle) <> "" Then
    7.    
    8.         MsgBox "The File Has Been Detected As Already Existing", _
    9.         vbExclamation, "The Program Will End" 'File Detected
    10.    
    11.     Else
    12.    
    13.         On Error Resume Next 'Error Proof
    14.         FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
    15.         MsgBox "The File Was Successfully Copied", _
    16.         vbInformation, "Successful Copy" 'Display Message Box
    17.        
    18.     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:
    1. "C:\WINDOWS\system32\" & FilenameTitle

    anyway thanks guys, learn something new everyday

  12. #12
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    VB Code:
    1. On Error Resume Next 'Error Proof
    2.         FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
    3.         MsgBox "The File Was Successfully Copied", _
    4.         vbInformation, "Successful Copy" 'Display Message Box
    Bad bad bad bad.
    VB Code:
    1. On Error Goto ErrorHandler
    2.    If FileToBeCopied = "" Then
    3.    
    4.         MsgBox "You Need To Select A File To Automatically Install", _
    5.         vbInformation, "No File Selected!" 'No File Selected
    6.        
    7.     ElseIf Dir$("C:\WINDOWS\system32\" & FilenameTitle) <> "" Then
    8.    
    9.         MsgBox "The File Has Been Detected As Already Existing", _
    10.         vbExclamation, "The Program Will End" 'File Detected
    11.    
    12.     Else
    13.         FileCopy Text1.Text, Text3.Text 'Copy The File SOURCE, DESTINATION
    14.         MsgBox "The File Was Successfully Copied", _
    15.         vbInformation, "Successful Copy" 'Display Message Box
    16.     End If
    17.     Exit Sub
    18. ErrorHandler:
    19.     MsgBox "ERROR!!! " & err.description
    Also...
    VB Code:
    1. MsgBox "The File Was Successfully Copied", _
    2.         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:
    1. MsgBox "The File Was Successfully Copied", vbInformation, "Successful Copy" 'Display Message Box
    Woka

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    Why span 2 lines???????
    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.

  14. #14
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    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:
    1. Dim strSQL As String
    2.     strSQL = "SELECT * " _
    3.     & "FROM Jobs " _
    4.     & "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:
    1. Dim strSQL As String
    2.     strSQL = "SELECT * " _
    3.     & "FROM Jobs " _
    4.     & "WHERE Completed = 1 " _
    5.     & "AND UserID = 3 "
    If you did:
    VB Code:
    1. Dim strSQL As String
    2.     strSQL = "SELECT * "
    3.     strSQL = strSQL & "FROM Jobs "
    4.     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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    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

  16. #16
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Err.Clear

    After you've handled the error, otherwise another active error handler in the calling subs will pick up the error.

  17. #17

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    119
    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

  19. #19
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    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?

  20. #20
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    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.

  21. #21

  22. #22
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015
    Aha! Grazie.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width