Hello Everybody,
ANybody know how can i get only the path to the file?
Example:
D:\Lucas\MyDocuments\Tool\Test.txt
i want to get only --> D:\Lucas\MyDocuments\Tool\
------------------------
I have a code that get only the Text.txt and only Text.
But was not me that created.
Thank for Helps =D
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Solved by bojko108. Go to Solution.
Use this function
Function GetPathName(strFile As String) As String
'return the path for a file (folder only)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetPathName = fso.GetParentFolderName(strFile)
End Function
Variable FileName holds full path name + name of the file
Dim pat As String
pat = GetPathName(FileName) ' return only pat to the folder
Sorry Bojko,
Buy is there another easier way to find the path?
I dont know how to insert this function and code in my project =/
i have listBox with full path and file.
i want to get the path in "i" position.
But im so noob in VBA =(
O,
I think i inserted the solution in my project,
Im doing some tests and post if it works good =D
EDIT.
It didn't work =/
when i put in the msgbox to show appears nothing
EDIT 2
Now it works,
I have put in the wrong place xD
Although the code (of using Scripting.FileObject) may works, it is very poor programming choice. Notice that I said "may work"? It means that it may not work. Scripting component is not meant to be used in desktop app, such as AutoCAD. It comes in different versions in different Windows OS. Some strictly managed network domain sees it as an security hole to install it on every users' computer and may disable it by domain policy.
The more basic thing is why to introduce an unnecessary external dependency to your code (not to mention the external dependency itself is not a good choice, as above described), when a task (getting a path from a fully pathed file name) is so simple string parsing work?
So, if you need to get path from something like "C:\folder1\fodler2\filename.txt", simply find the position of last "\", and then cut the string off from that point:
Dim fullPath As string
Dim purePath As string
fullPath="C:\Folder1\Folder2\fileName.txt"
Dim n As Interger
n=InStrRev(fullPath,"\")
purePath=Left(fullPath,n) ''the path is ended with "\"
'Or
purePath=Left(fullPath,n-1) ''Path is ended without "\"
Norman Yuan