Jump to content

Getting a specific string from a file path


Recommended Posts

Posted

Hi,

 

I'm using

(setq Cfilepath (getvar 'DWGPREFIX))

to get my current filepath and i would like to get a specific string from it.

 

Filepath : "L:\\ECHAFAUDAGES (L2)\\CLIENTS\\Name of client\\Project name\\ingénierie\\PlanCad\\"

 

Thing is i'm not sure what to use since

 

Name of client never has the same lenght, and same for project name.

 

So i can't use substr.

 

What function should I be looking for ?

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • CadFrank

    12

  • MSasu

    6

  • BlackBox

    4

  • Tharwat

    2

Top Posters In This Topic

Posted

What are you looking in fact to achive? To check if a given string (i.e. client name) is present or to retrive the name of client (the third folder from root)?

Posted

Well Client name will always be present. I just want to retrieve the project name to print it in a .txt file

Posted

Then you may use this function:

;;++++++++++++++++++
;; parser by CAB
;;++++++++++++++++++
(defun sparser (str delim / ptr lst)
 (while (setq ptr (vl-string-search delim str))
  (setq lst (cons (substr str 1 ptr) lst))
  (setq str (substr str (+ ptr 2)))
 )
 (reverse (cons str lst))
)

Use it like:

(nth 4 (SPARSER (getvar "DWGPREFIX") "\\"))

For sure this presume that project name will be always the 4th folder from root.

Posted

@Tharwat, I'm afraid that SUBSTR cannot help in OP's case.

Name of client never has the same lenght, and same for project name.
Posted
@Tharwat, I'm afraid that SUBSTR cannot help in OP's case.

I was preparing the post when the OP posted that more clarification . so I will take it back if it is not helpful at all .

Posted
For sure this presume that project name will be always the 4th folder from root.

 

Well actually the fifth :P

 

But indeed it works :D

Posted (edited)

 

So i can't use substr.

 

 

Naw it's ok Tharwat i just think you didn't read my post fully :P

Edited by CadFrank
Posted

You said "project name", right? It may be the 5th item in the list, but please don't miss that the first index in a list is always 0; so the 5th entry correspond in fact to index 4.

Posted

Well I entered 5 in the code

 

(nth 5 (SPARSER (getvar "DWGPREFIX") "\\"))

 

And it game me the project name ??

Posted

I have based my comments on the example path from first post; to see how the path is divided in items call only:

(SPARSER (getvar "DWGPREFIX") "\\")

Anyway, I just wanted to ensure that you got the principle right; for sure should use 5 if that is the apropriate index.

Posted

where does the argument delim get his information.. can't seem to understand the code...

Posted
where does the argument delim get his information.. can't seem to understand the code...

 

I've just stumbled upon this thread, but as I understand it, you supply the delim value as an argument of the Sparser function.

 

 

 

FWIW - I offered this adaptation over at AUGI some time ago:

 

 

(vl-load-com)
(defun _FilePath->List (filePath / i folder folders)
 (setq filePath (vl-string-translate "\\" "/" filePath))
 (while (setq i (vl-string-search "/" filePath))
    (setq folders (cons (setq folder (substr filePath 1 i)) folders))
    (setq filePath (substr filePath (+ 2 i)))
    )
 (reverse folders)
 )

 

Example:

_$ (_FilePath->List "G:\\745 The name of the Project\\74534\\drawings\\")
("G:" "745 The name of the Project" "74534" "drawings")
_$ 

 

 

The biggest difference is that it (my offering) will accept both double back-slashes "\\" and / or single forward-slash "/" for the file path string as an argument, and returns a list of strings, where each string represents a folder in the supplied path.

 

HTH

Posted

Waaaa! this is so complicated !!! lol

 

Well I'm just trying to evaluate how you got to think of writting that code..

 

I'm basicly trying to make a login log out code to know the amount of time i spend on a projet hehe!!

Posted

Waaaa! this is so complicated !!! lol

 

Well I'm just trying to evaluate how you got to think of writting that code..

 

Removing the learning curve, it's really simple code at it's root... The limited use for adding the feature to accept both delimiters while applicable to this situation specifically, was certainly not intended to confuse anyone.

 

I'm basicly trying to make a login log out code to know the amount of time i spend on a projet hehe!!

 

If only needing to account for what drawings are opened, at what time they were opened, and when they were closed... And the built-in log system is insufficient... You could use a simple DocManager Reactor, with the :vlr-DocumentCreated, and :vlr-DocumentToBeDestroyed Events respectively.

 

If you're more adept at coding .NET, then look into using a similar concept, with a static DocumentCollection Event handler.

 

If you require context... Meaning was anything actually done... That's another layer of complexity, but doable.

 

If you're wanting to add a filter for what commands were performed, and how one would quantify was deleting that line, or drawing those three circles beneficial to the project... That's a whole different level of complexity entirely, and perhaps one that is impossible to identify by consensus.

Posted

Yeah, I guess it's not meant to be a hard code.

 

The only coding I know a little of is Autolisp. Think is I'm writting codes and sometimes I ask for help and I get answer in witch I do not know the meaning or even how to read the code. That's like my problem, I'm trying to learn Autolisp but I'm not sure how to approach the coding for me to understand it. Or maybe I'm looking at it a little to close hehe !! and the answer is in my face who knows. I'm just trying to figure it out !

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...