Write folder files to txt file help

Write folder files to txt file help

DC-MWA
Collaborator Collaborator
1,429 Views
9 Replies
Message 1 of 10

Write folder files to txt file help

DC-MWA
Collaborator
Collaborator

Hello,

I have a lisp that we use for a detail library. Keeping the txt file it references up to date as we add and remove details is time consuming to say the least.

This morning I've been trying to peice together something to make this easier. A way to process the folders and creating txt files in the folder that I could cut and paste in to the master control file.

 

The program reads a text file that is formatted like this:

("Folder Name" "Drawing Name" "Drawing Name" "Description")

Sample:

("10 SITE" "10-01" "10-01" "Detail 10-01")
("10 SITE" "10-02" "10-02" "Detail 10-02")
("10 SITE" "10-03" "10-03" "Detail 10-03")
("10 SITE" "10-04" "10-04" "Detail 10-04")
("10 SITE" "10-05" "10-05" "Detail 10-05")

 

So the user selects  a folder,

The list of drawings (.dwg files) is created,

The list and folder name are used to create a .txt file formatted as described above.

 

I started "franken-lisping" this morning and this is what I have so far.

 

(defun c:parts (/ )

(setq getfolderpath (strcat (acet-ui-pickdir "Select Detail Folder to Process" "F:\\MWA Work\\PRODUCTION\\Details")"\\"))
(setq
foldername (vl-string-left-trim "\\" getfolderpath)
foldername (substr foldername 1 (vl-string-position (ascii "\\") foldername 0 T))
penultimatefolder (substr foldername (+ 2 (vl-string-position (ascii "\\") foldername 0 T)))
)

(setq Files_Folder (vl-directory-files getfolderpath "*.dwg"))

(setq text "")
(if (/= (length Files_Folder) 0)
(repeat (setq N (length Files_Folder))
(setq e (nth (setq N (- N 1)) Files_Folder))

(setq e (vl-filename-base e))

(setq text (strcat text "\(\"" penultimatefolder "\" " e "\" " "\"" e "\" " "\"Detail " e "\"\)" "\\P"));;;good

);repeat

(princ "\nDoes not containing any *.DWG File.")
)
(if (/= (length Files_Folder) 0)
(command "_.MTEXT" pause pause text "")
)

(princ)
)

 

I had planned on changing the mtext portion to write to a txt file instead.

My lisp skills are limited so I apologize for the crappy autolisping.

Thank you in advance for any assistance received.

0 Likes
Accepted solutions (1)
1,430 Views
9 Replies
Replies (9)
Message 2 of 10

pbejse
Mentor
Mentor

@DC-MWA wrote:

The program reads a text file that is formatted like this:

("Folder Name" "Drawing Name" "Drawing Name" "Description")

Sample:

("10 SITE" "10-01" "10-01" "Detail 10-01")


 

The result you posted is the product of the program after reading the  contents of the text file?

Or are you saying the content is exactly like that on the text file?

 

pbejse_0-1630348175053.png

 

Also, the "Description"  value is always "Detail <filename>" ?

 

0 Likes
Message 3 of 10

DC-MWA
Collaborator
Collaborator

Yes

Yes

0 Likes
Message 4 of 10

Anonymous
Not applicable

FYI, when posting code here use the "Insert/Edit code sample" (looks like "</>") from the toolbar.  Then it looks like this:

(defun blah ( / oopsie )
  (princ "Blah!")
   (setq 
      oopsie (getvar "LASTVAR")
   )
   (setvar "LASTVAR" "New Blah!")
)

 

Preserves formatting such as indents, and helps it stand out from the body of the post.

0 Likes
Message 5 of 10

DC-MWA
Collaborator
Collaborator

got it.

0 Likes
Message 6 of 10

pbejse
Mentor
Mentor

@DC-MWA wrote:

Yes

Yes


Yes to what @DC-MWA  ? 

  1. The result you posted is the product of the program after reading the  contents of the text file?
  2. Or are you saying the content is exactly like that on the text file?
  3. The "Description"  value is always "Detail <filename>" ? 

 

 

0 Likes
Message 7 of 10

DC-MWA
Collaborator
Collaborator
  1. The result you posted is the product of the program after reading the  contents of the text file? No. What I posted is what I need the text file to be.
  2. Or are you saying the content is exactly like that on the text file?Yes, I need the ofrmat to be like that in the text file created.
  3. The "Description"  value is always "Detail <filename>" ? Yes, the description is always "Detail <filename>"
0 Likes
Message 8 of 10

pbejse
Mentor
Mentor
Accepted solution

@DC-MWA wrote:

 

Yes, I need the ofrmat to be like that in the text file created.

Yes, the description is always "Detail <filename>"


Here's a quick mod of your code:

 

(Defun c:parts ( / AllDrawingFiles FNBase WriteToThis penultimatefolder)
(if
  (and
   	(setq f (acet-ui-pickdir "Select project Folder"
		  			(if f f (getvar 'dwgprefix))))
	(setq AllDrawingFiles (vl-directory-files f "*.dwg"))
	(setq FNBase (strcat f "\\" (vl-filename-base (getvar 'dwgname)) ".txt"))
	  )
	(progn
	  (setq WriteToThis (open FNBase "wr"))
	  (setq penultimatefolder (vl-filename-base f))  
		(foreach itm AllDrawingFiles
		  (setq fn (vl-filename-base itm))
		  (write-line (strcat "\(\"" penultimatefolder
				      "\" " fn "\" " "\"" fn "\" " "\"Detail "
				      			fn "\"\)") WriteToThis)
		  )		
		(close WriteToThis)
	  (startapp "Notepad" FNBase)
		  )
	  )
  (princ)
    )

 

HTH

 

0 Likes
Message 9 of 10

DC-MWA
Collaborator
Collaborator

This is what I was looking for. Is it possible to remove files in the folder that are named "0A*.dwg" from the list?

0 Likes
Message 10 of 10

DC-MWA
Collaborator
Collaborator

I figured out the file removal. 

This awesome!

Thank you for your assistance!!!

0 Likes