|
-
May 19th, 2011, 03:20 AM
#1
[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
-
May 19th, 2011, 05:14 AM
#2
Lively Member
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?
-
May 19th, 2011, 05:43 AM
#3
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
-
May 19th, 2011, 05:59 AM
#4
Lively Member
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
-
May 19th, 2011, 06:35 AM
#5
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
-
May 19th, 2011, 08:55 AM
#6
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
-
May 19th, 2011, 07:07 PM
#7
Re: Retrieve the name of the last folder in a path
 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.
 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.
 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
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
-
May 20th, 2011, 06:19 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|