VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get Path of file

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
2434 Views, 5 Replies

Get Path of file

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

5 REPLIES 5
Message 2 of 6
bojko108
in reply to: Anonymous

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

Message 3 of 6
Anonymous
in reply to: bojko108

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 =(

Message 4 of 6
Anonymous
in reply to: Anonymous

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

Message 5 of 6
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 6 of 6
Anonymous
in reply to: norman.yuan

Thank you too Norman,

 

I liked your code =D

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost