[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
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?
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?
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
Re: Retrieve the name of the last folder in a path
Is there a way to do it using api calls?
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
Re: Retrieve the name of the last folder in a path
Quote:
Originally Posted by
VBClassicRocks
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
I finally managed to get the code working it moves both the folder and files as excepted.
vb Code:
Private Function GetPath()
If Not Dir$(App.Path & "\xlive.dll") <> "" Then
source = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
destination = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
Else
source = GetSpecialFolder(CSIDL_LOCAL_APPDATA) & "\Rockstar Games\GTA IV\"
destination = GetSpecialFolder(CSIDL_PERSONAL) & "\Rockstar Games\GTA IV\"
End If
Call Copy(source)
End Function
Private Sub Copy(source)
Dim c As String
c = source & "savegames\user_e00006645743f1a9"
destination = destination & "savegames\user_e00006645743f1a9"
Call VBCopyFolder(c, destination)
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
VB Code:
Dim Mypath as string, MyName as String,iCount as integer
iCount=0
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> "." And MyName <> ".." Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' Display entry only if it
iCount=iCount+1
End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop
debug.print "No.of Folders in the selected path : " & iCount
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