Exporting File Sizes before and after purging to CSV

Exporting File Sizes before and after purging to CSV

Anonymous
Not applicable
1,241 Views
8 Replies
Message 1 of 9

Exporting File Sizes before and after purging to CSV

Anonymous
Not applicable

We have many thousands of drawing. We have lisp routine to purge the drawing with out our intervention. What we need is LISP routine to generate a report in CSV with following columns. I know how to do this to generate .txt file but don't know the .CSV one

 

Date , File Name,  File Size before,  File Size After, Change in file size.

 

Appreciate help on this.

 

Thanks

Hardik

0 Likes
Accepted solutions (3)
1,242 Views
8 Replies
Replies (8)
Message 2 of 9

krzysztof.psujek
Advocate
Advocate
Hi, you can use LeeMac function to export data you have to csv.
Look here for the function and description

http://www.lee-mac.com/writecsv.html.

Chris
0 Likes
Message 3 of 9

pbejse
Mentor
Mentor

@Anonymous wrote:

 

Date , File Name,  File Size before,  File Size After, Change in file size.

 

Thanks

Hardik


Very close, check it out.

 

 Drawing Purge [ Ajilal Vijayan ] with Export To CSV option 

 

 

options.png

 

 

Message 4 of 9

roland.r71
Collaborator
Collaborator
Accepted solution

@Anonymous wrote:

We have many thousands of drawing. We have lisp routine to purge the drawing with out our intervention. What we need is LISP routine to generate a report in CSV with following columns. I know how to do this to generate .txt file but don't know the .CSV one

 

Date , File Name,  File Size before,  File Size After, Change in file size.

 

Appreciate help on this.

 

Thanks

Hardik



A.csv file basically is a .txt file

 

The only difference is that you 'separate' parts of the file by a special sign. The C stands for 'comma' but in reality people use all sorts of chars.

I personally prefer to use ; others use [tab].

 

So, to write a csv file is the same as writing a txt file.

 

Except you write each line as a database record, with Comma Seperated Value's.

 

Top row usually contains the headers.

So, you can litterary write your line:

Date , File Name,  File Size before,  File Size After, Change in file size

 

to a .txt (ASCII) file, which you just name .csv

 

Using the same method:

(setq csvfile (open "c:/temp/mydata.csv" "w"))

(write-line "Date , File Name,  File Size before,  File Size After, Change in file size" csvfile)

(close csvfile)

 

Message 5 of 9

Anonymous
Not applicable

Thanks, I have routine now but it repeats the header row for each file. Can you help with that?

 

0 Likes
Message 6 of 9

roland.r71
Collaborator
Collaborator

You probably included it inside the same loop where you write the lines.

 

First open the file, then write the headers.

After that, start any while or foreach loop to write the data.

Finally close the file.

Message 7 of 9

roland.r71
Collaborator
Collaborator

Example with writing separate header:

(setq dataList (list "18-9-2017, my1st.dwg, 1 kB, 201 kB, 200 kB"
                     "19-9-2017, my2nd.dwg, 100 kB, 250 kB, 150 kB"
                     "19-9-2017, my3rd.dwg, 150 kB, 100 kB, -50 kB")
)
(setq csvFile (open "C:/temp/myCSV.csv" "w"))
(write-line "Date , File Name,  File Size before,  File Size After, Change in file size" csvfile)
(foreach dataLine dataList
   (write-line dataLine csvFile)
)
(close csvFile)

Example with all data combined in 1 list:

(setq dataList (list "Date , File Name,  File Size before,  File Size After, Change in file size"
                     "18-9-2017, my1st.dwg, 1 kB, 201 kB, 200 kB"
                     "19-9-2017, my2nd.dwg, 100 kB, 250 kB, 150 kB"
                     "19-9-2017, my3rd.dwg, 150 kB, 100 kB, -50 kB")
)
(setq csvFile (open "C:/temp/myCSV.csv" "w"))
(foreach dataLine dataList
   (write-line dataLine csvFile)
)
(close csvFile)

Of course, the var dataList can be a list of lists.

 

(list
   (list "Date" "Filename" "Filesize before" "Filesize after" "Change in filesize")
   (list "18-9-2017" "my1st.dwg" "1 kB" "201 kB" "200 kB")
)

etc.

 

In which case you might need to add a "foreach" or "while" loop for each item and add the separator inbetween.

Or just:

(write-line (strcat (nth 0 dataLine) "," (nth 1 dataLine) "," (nth 2 dataLine) "," (nth 3 dataLine) "," (nth 4 dataLine)) csvFile)
Message 8 of 9

roland.r71
Collaborator
Collaborator
Accepted solution

If you want to write to the file from each dwg, use something like:

 

(setq date       "19-09-2017"
      filename   "my1st.dwg"
      sizeStart  "1 kB"
      SizeEnd    "201 kB"
      difference "200 kB"
dataLine (strcat date "," filename "," sizeStart "," sizeEnd "," difference) ) (setq csvFile "C:/temp/myCSV.csv") (if (findfile csvFile) ; if it exists (open csvFile "a") ; append to file ; else (progn (open csvFile "w") ; create file & add headers (write-line "Date , File Name,  File Size before,  File Size After, Change in file size" csvfile) ) ) (write-line dataLine csvFile) ; write data to file
(close csvFile)

This will create the file, if it doesn't exist & add the headers before writing the data.

Otherwise it will just append the new data to the existing file.

Message 9 of 9

Anonymous
Not applicable
Accepted solution

Thanks roland.r71 for help. 

 

You guided me correctly, the code in you solution was giving error for CSV file path. Here is final code that worked for me like a champ.

 

(defun c:writeCSV ()
	(setq 	date (TODAY)
		TIMEin (TIME)
		dwgfn (getvar 'DWGNAME)
		dwgdp (getvar 'DWGPREFIX)
		dwgfp (strcat dwgdp dwgfn)
		fs1 (vl-file-size dwgfp)
 	);setq

	(setq file (findfile "C:/Temp/LOG.csv"))
	(if (not file)
		(write-line "Date , timein, timeout, Dwg_Name, File_size_1,  File_Size_2, File_Size_Change" (open "C:/Temp/LOG.csv" "w"))	
		(if (/= dwgfn "Drawing.dwg")
		(progn  
			(setq file (findfile "C:/Temp/LOG.csv"))
			(setq csvfp (open file "a"))
		    (Command "qsave")
			(setq fs2 (vl-file-size dwgfp))
			(setq TIMEout (TIME))
			(setq d1 (strcat date "," TIMEin "," TIMEout "," dwgfn ","  (itoa fs1) "," (itoa fs2) "," (itoa (- fs1 fs2))));setq
			(princ d1 csvfp)
			(princ "\n" csvfp)
			(close csvfp)
			(princ (strcat "\nLogged out at : " TIMEout))
        );progn
	);if
	
	);if
	
	(princ)
	;(command "close")
)

 

0 Likes