Results 1 to 8 of 8

Thread: [RESOLVED] Retrieve the name of the last folder in a path

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Retrieve the name of the last folder in a path

    Hi,

    How would I retrieve the name of the last folder in a path if the last folder changes?

    Simply put I have a path:

    GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\savegames\" and want to retrieve the filename of the folder/s in the savegames folder automatically.

    I have tried this although, it only seems to work with a static path.

    Thanks,


    Nightwalker
    Last edited by Nightwalker83; May 19th, 2011 at 10:41 PM. Reason: Not resolved
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: Retrieve the name of the last folder in a path

    How do you recognize your Last folder...by alphabetically...? or date of creation...? or any other way?

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Retrieve the name of the last folder in a path

    Alphabetically! However, if the folders are sorted by date, etc finding them alphabetically won't work. So, I was thinking retrieving the name in a loop might?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: Retrieve the name of the last folder in a path

    If you want to get the last folder alphabetically or date of creation or date of last accessed, date of last modified, size etc...use the following:

    reference to 'microsoft scripting runtime->Scripting.FileSystemObject->folder


    or

    simply put dirlistbox and set desired path: you will get the folders alphabetically arranged...
    move to last item and get the folder name

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Retrieve the name of the last folder in a path

    Is there a way to do it using api calls?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Retrieve the name of the last folder in a path

    Isn't it just string manipulation?
    Code:
    Private Function LastFolderName(Path As String) As String
     Dim a() As String
     If Right$(Path, 1) = "\" Then
      Path = Left$(Path, Len(Path) - 1)
     End If
     a = Split(Path, "\")
     LastFolderName = a(UBound(a))
    End Function

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Retrieve the name of the last folder in a path

    Quote Originally Posted by VBClassicRocks View Post
    Isn't it just string manipulation?
    Code:
    Private Function LastFolderName(Path As String) As String
     Dim a() As String
     If Right$(Path, 1) = "\" Then
      Path = Left$(Path, Len(Path) - 1)
     End If
     a = Split(Path, "\")
     LastFolderName = a(UBound(a))
    End Function
    Thanks, that is exactly what I'm looking for.

    Edit:

    I may have replied too soon.

    Quote Originally Posted by Nightwalker83 View Post
    I finally managed to get the code working it moves both the folder and files as excepted.

    vb Code:
    1. Private Function GetPath()
    2.     If Not Dir$(App.Path & "\xlive.dll") <> "" Then
    3.         source = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
    4.         destination = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
    5.     Else
    6.         source = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
    7.          destination = GetSpecialFolder(CSIDL_PERSONAL) & "\Rockstar Games\GTA IV\"
    8.     End If
    9.      Call Copy(source)
    10. End Function
    11.  
    12. Private Sub Copy(source)
    13. Dim c As String
    14. c = source & "savegames\user_e00006645743f1a9"
    15. destination = destination & "savegames\user_e00006645743f1a9"
    16. Call VBCopyFolder(c, destination)
    17. End Sub
    See how the above path has a hard-coded folder "user_e00006645743f1a9"? I want to retrieve the name of that folder without hard coding it.

    Say if c = source & "savegames\" I want to retrieve the names of the folders in the savegames folder (example: user_e00006645743f1a9).

    I apologize for this post if your code does what I am asking it is I have stuffed it up, checking LastFolderName via msgbox(LastFolderName) displays a blank message box.

    Edit 2:

    This is what I was after.

    Quote Originally Posted by faisalkm View Post
    VB Code:
    1. Dim Mypath as string, MyName as String,iCount as integer
    2. iCount=0
    3. MyPath = "c:\"   ' Set the path.
    4. MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
    5. Do While MyName <> ""   ' Start the loop.
    6.    ' Ignore the current directory and the encompassing directory.
    7.    If MyName <> "." And MyName <> ".." Then
    8.       ' Use bitwise comparison to make sure MyName is a directory.
    9.       If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    10.          Debug.Print MyName   ' Display entry only if it
    11.          iCount=iCount+1
    12.       End If   ' it represents a directory.
    13.    End If
    14.    MyName = Dir   ' Get next entry.
    15. Loop
    16.  
    17.  
    18. debug.print "No.of Folders in the selected path : " & iCount
    Last edited by Nightwalker83; May 19th, 2011 at 11:47 PM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] Retrieve the name of the last folder in a path

    I know you have your solution but here is an example using the file system object
    Code:
    Private Sub Command1_Click()
    Dim fso As Object
    Dim fld As Object
    Dim subFld As Object
    Dim strPath As String
    
        strPath = "C:\Inetpub\wwwroot"
        
        'Create the filesystem object
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        'Load the parent folder path
        Set fld = fso.GetFolder(strPath)
        
        Debug.Print "No.of Folders in the selected path : " & fld.SubFolders.Count
        
        'Loop through the parent folder and the the subfolder names
        For Each subFld In fld.SubFolders
            Debug.Print subFld.Name
        Next subFld
        
        'Cleanup
        Set fld = Nothing
        Set fso = Nothing
        
    End Sub

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