Closed Thread
Results 1 to 2 of 2

Thread: problem with recursive function

  1. #1
    reniery is offline Learning Programmer
    Join Date
    May 2008
    Posts
    45
    Rep Power
    0

    problem with recursive function

    So i'm helping my bro with this hmwk he has and he did this function. Problem is it doesn't do what he wants and I can't figure out why. The part where he does some string manipulation is completely wrong but I'm sure he can fix it. For some reason the function returns an empty string when it should return a string. So what's wrong with this function (apart from the string manipulation ) why does it return an empty string.

    What the function is suppose to do is check if a file exists in a folder, if it does exists change the filename to avoid a file already exists error when moving files between folders.

    Code:
    Function FileExists(ByVal carpetaRaiz As String, ByVal sFileName As String) As String
            
            Dim filename As String
            Dim part1 As String
            Dim part2 As String
            Dim part3 As String
            Dim part4 As String
            Dim pos1 As Integer
            Dim pos2 As Integer
            Dim length As Integer
            
            Dim number As Integer
            
            If Dir(carpetaRaiz & "\" & sFileName) <> "" Then 'file exists
                
                length = Len(sFileName)
                
                If InStr(sFileName, "-") <> 0 Then
                    pos1 = InStr(sFileName, "-")
                    pos2 = InStr(sFileName, ".")
                    
                    part1 = Mid(sFileName, 1, pos1 - 1)
                    part2 = Mid(sFileName, pos1 + 1, pos2 - 1)
                    part3 = Mid(sFileName, pos2 + 1)
                    
                    If IsNumeric(part2) Then
                    number = CInt(part2)
                    number = number + 1
                    
                    part4 = Mid(part2, 1, Len(part2) - 1)
                    
                    part2 = CStr(number)
                    
                    part2 = part4 & part2
                    
                    End If
                                    
                Else
                    pos1 = InStr(sFileName, ".")
                    
                    part1 = Mid(sFileName, 1, pos1 - 1)
                    part2 = "002"
                    part3 = Mid(sFileName, pos1 + 1)
                    
                End If
                
                filename = part1 & "-" & part2 & "." & part3
                
                FileExists carpetaRaiz, filename
                            
            Else ' doesnt exist
                FileExists = sFileName
                'sfile.Move (carpetaRaiz & "\" & sfile.Name)
            End If
            
    End Function

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: problem with recursive function

    What's this line supposed to do?

    Code:
     
    FileExists carpetaRaiz, filename

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 05-11-2011, 10:29 PM
  2. recursive function
    By reference in forum C# Programming
    Replies: 1
    Last Post: 11-15-2010, 11:04 AM
  3. Can someone help explain this recursive function
    By flodywan in forum C and C++
    Replies: 4
    Last Post: 04-19-2010, 05:10 PM
  4. Problem with recursive function.
    By posto2012 in forum C and C++
    Replies: 3
    Last Post: 12-05-2009, 06:19 AM
  5. recursive function
    By pretty.gal13 in forum C# Programming
    Replies: 5
    Last Post: 03-06-2009, 08:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

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