Results 1 to 9 of 9

Thread: Code Help! (If File Exist)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Code Help! (If File Exist)

    Below is a code that i have put together that has some problems. I'm making a recreation of a startbar in my application. The code checks to see if a temp file exist with the caption of another task. The whole code checks to see if it exist or not. Here is my problem when i open up a task for example task 1 it shows the task "perfect" but when i open up the second task it seems like it does not check to see if it exist at all it just uses the first task. Why is that? My COde:

    Code:
    'Below Is The Code That Generates And Checks The TaskBar
    'Check To See If The Task Is Being Used
    Dim IntCheckFile As Integer
        Dim fileNum As Integer
        IntCheckFile = FileExists("\CypexOS\MyServer\CypexOS\StartMenu\Task1.ini")
        Select Case IntCheckFile
          Case -1
          'Nope It Dosen't Exist!
          '---------------------------------------------------
          'Since The Task Dosen't Exist Save The Caption
              Dim hFile As Long
       Dim sFilename As String
    
       sFilename = App.path & ("\CypexOS\MyServer\CypexOS\StartMenu\Task1.ini")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Me.Caption
       Close #hFile
       '---------------------------------------------------
       'Since The Caption Has Been Saved Get The Task Image
            StartbarMen.Task1.Picture = LoadPicture(App.path & "\CypexOS\Start\Tasks\TaskCYPEX.bmp")
            'Since The Task Has Been Set Set The Caption
        fileNum = FreeFile
        Open App.path & "\CypexOS\MyServer\CypexOS\StartMenu\Task1.ini" For Input As #fileNum
        ' Read the contents of the file into TextBox1
        StartbarMen.Task1C.Caption = Replace$(Input(LOF(fileNum), fileNum), vbCrLf, "")
        ' Close the file
        Close fileNum
        'Make Sure The Task is Visable
        StartbarMen.Task1.Visible = True
        StartbarMen.Task1C.Visible = True
          Case 0
          Case 1
          'So The Task Exist Look For The New Task!
          IntCheckFile = FileExists("\CypexOS\MyServer\CypexOS\StartMenu\Task2.ini")
        Select Case IntCheckFile
          Case -1
          'Nope It Dosen't Exist!
          'Since The Task Dosen't Exist Save The Caption
    
       sFilename = App.path & ("\CypexOS\MyServer\CypexOS\StartMenu\Task2.ini")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Me.Caption
       Close #hFile
       'Since The Caption Has Been Saved Get The Task Image
            StartbarMen.Task2.Picture = LoadPicture(App.path & "\CypexOS\Start\Tasks\TaskCYPEX.bmp")
            'Since The Task Has Been Set Set The Caption
        fileNum = FreeFile
        Open App.path & "\CypexOS\MyServer\CypexOS\StartMenu\Task2.ini" For Input As #fileNum
        ' Read the contents of the file into TextBox1
        StartbarMen.Task2C.Caption = Replace$(Input(LOF(fileNum), fileNum), vbCrLf, "")
        ' Close the file
        Close fileNum
        'Make Sure The Task is Visable
        StartbarMen.Task2.Visible = True
        StartbarMen.Task2C.Visible = True
          Case 0
          Case 1
          'Nope It Dosen't Exist!
          'Since The Task Dosen't Exist Save The Caption
    
       sFilename = App.path & ("\CypexOS\MyServer\CypexOS\StartMenu\Task3.ini")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Me.Caption
       Close #hFile
       'Since The Caption Has Been Saved Get The Task Image
            StartbarMen.Task3.Picture = LoadPicture(App.path & "\CypexOS\Start\Tasks\TaskCYPEX.bmp")
            'Since The Task Has Been Set Set The Caption
        fileNum = FreeFile
        Open App.path & "\CypexOS\MyServer\CypexOS\StartMenu\Task3.ini" For Input As #fileNum
        ' Read the contents of the file into TextBox1
        StartbarMen.Task3C.Caption = Replace$(Input(LOF(fileNum), fileNum), vbCrLf, "")
        ' Close the file
        Close fileNum
        'Make Sure The Task is Visable
        StartbarMen.Task3.Visible = True
        StartbarMen.Task3C.Visible = True
        Case 0
        Case 1
        'Nope It Dosen't Exist!
          'Since The Task Dosen't Exist Save The Caption
    
       sFilename = App.path & ("\CypexOS\MyServer\CypexOS\StartMenu\Task4.ini")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Me.Caption
       Close #hFile
       'Since The Caption Has Been Saved Get The Task Image
            StartbarMen.Task4.Picture = LoadPicture(App.path & "\CypexOS\Start\Tasks\TaskCYPEX.bmp")
            'Since The Task Has Been Set Set The Caption
        fileNum = FreeFile
        Open App.path & "\CypexOS\MyServer\CypexOS\StartMenu\Task4.ini" For Input As #fileNum
        ' Read the contents of the file into TextBox1
        StartbarMen.Task4C.Caption = Replace$(Input(LOF(fileNum), fileNum), vbCrLf, "")
        ' Close the file
        Close fileNum
        'Make Sure The Task is Visable
        StartbarMen.Task4.Visible = True
        StartbarMen.Task4C.Visible = True
        Case 0
        Case 1
        'Nope It Dosen't Exist!
          'Since The Task Dosen't Exist Save The Caption
    
       sFilename = App.path & ("\CypexOS\MyServer\CypexOS\StartMenu\Task5.ini")
       
      'obtain the next free file handle from the
      'system and and save the text box contents
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, Me.Caption
       Close #hFile
       'Since The Caption Has Been Saved Get The Task Image
            StartbarMen.Task5.Picture = LoadPicture(App.path & "\CypexOS\Start\Tasks\TaskCYPEX.bmp")
            'Since The Task Has Been Set Set The Caption
        fileNum = FreeFile
        Open App.path & "\CypexOS\MyServer\CypexOS\StartMenu\Task5.ini" For Input As #fileNum
        ' Read the contents of the file into TextBox1
        StartbarMen.Task5C.Caption = Replace$(Input(LOF(fileNum), fileNum), vbCrLf, "")
        ' Close the file
        Close fileNum
        'Make Sure The Task is Visable
        StartbarMen.Task5.Visible = True
        StartbarMen.Task5C.Visible = True
        Case 0
        Case 1
        End Select
       End Select
      End Select
     End Select
    End Select

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Code Help! (If File Exist)

    currently your code always checks task1.ini, look like you need to find the number of current task first, we can't see the code for fileexists function, but looks like it always returns false, you should probably be using an array or collection to keep track of the tasks, that way the ubound of the array +1 would be the next task
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Re: Code Help! (If File Exist)

    Ohh My Badd Here it Is:
    Code:
    Function FileExists(strFile As String) As Integer
        Dim lSize As Long
    
        On Error Resume Next
        '* set lSize to -1
        lSize = -1
        'Get the length of the file
        lSize = FileLen(strFile)
        If lSize = 0 Then
            '* File is zero bytes and exists
            FileExists = 0
        ElseIf lSize > 0 Then
            '* File Exists
            FileExists = 1
        Else
            '* Does not exist
            FileExists = -1
        End If
    End Function

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Code Help! (If File Exist)

    Code:
    Function FileExists(strFile As String) As Boolean
        FileExists = LenB(Dir$(strFile, vbSystem Or vbHidden)) <> 0&
    End Function
    True = -1 = file exists
    False = 0 = file does not exist

    Check for file length separately after you know the file exists.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Code Help! (If File Exist)

    FileExists = LenB(Dir$(strFile, vbSystem Or vbHidden)) <> 0&
    as the path is removed by the dir function, i guess it will only work with files in the current path?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Code Help! (If File Exist)

    It works if the file exists as the filename is returned in the path if it exists there. Dir$ returns empty if the given string is not a valid path or file. Just for an example:

    Code:
    MsgBox LenB(Dir$("c:\windows\system32\comctl32.dll", vbSystem Or vbHidden)) <> 0&
    True

    Code:
    MsgBox LenB(Dir$("c:\windows\system32\comctl32.notgonnahappen", vbSystem Or vbHidden)) <> 0&
    False


    LenB is just the quickest way to see if we got any return string, and since the return string is always valid there should be no problem. Except that Unicode filenames aren't supported, but that is an entirely different story.

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Code Help! (If File Exist)

    Dir gains no speed from the $; it always uses strings regardless.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Re: Code Help! (If File Exist)

    So why is my code not working properly?

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Re: Code Help! (If File Exist)

    I'm confused. Everyone semmed top get side trackted. Wht makes my code not work properly?

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