Jump to content

copying into the clipboard entire file (many rows)


Recommended Posts

Posted

Is it possible to read an entire file of simple txt (ascii) holding many lines of data produced from a while loop into the clipboard?

 

I ve found a routine that when given a string it will copy it into the clipboard. Problem is, how can I select the entire file (all lines) an put it in one variable and paste it into the clipboard? Or is there an other way?

 

I know the read-line command but this reads only one line, as far as I know...

Posted

its true read-line only reads one line at a time, if used in conjunction with a while loop and strcat you can read an entire text file and have be represented by one variable. (caution on 255 character length limit)

Posted

make sense but I think this way the contents in the clipboard will be one straight lengthy line. My intent is to have it as

 

1 2 4 20U

2 3 4 5N

3 4 4 5W

etc...

 

each line with a carriage return. Is it possible with read-line as you said?

Posted

Instead of creating a variable with a single line of the entire text file, you could create a list variable, with each item in the list being one line of the text file.

 

(setq file (open filename "r"))
(while (setq tline (read_line file))  ;this will stop at end of file
   (setq fileL (cons tline fileL))
)
(close file)
(setq fileL (reverse fileL))

Posted

It is possible... yes. And to separate each "line" of text you would...

 

(setq clipboardtxt (strcat clipboardtxt line "\n"))

 

From memory, I don't think you'll find a character limit problem either. That only applies to the TEXT entity, not the ability of a symbol to hold the string.

 

I am puzzled why you would want to though. If the data was coming from AutoCAD, being able to paste it elsewhere is handy. But when from a file? I have text editors to do that sort of thing. But it is possible. :)

Posted

Sorry I may have not explained it the way I should. I already have a while loop creating on each pass a line just like I wrote above. I get the file full of lines alright but instead on manually copying pasting the whole text from this file to an other software (pron to error process), I though I could have the lines instead of a file, in the clipboard.

 

After experimenting a bit I managed to do it as you say cwake.

Command

(setq clipboardtxt (strcat clipboardtxt line "\n"))

did the trick for me.

 

Thank you guys for your help.

Posted
Sorry I may have not explained it the way I should. I already have a while loop creating on each pass a line just like I wrote above. I get the file full of lines alright but instead on manually copying pasting the whole text from this file to an other software (pron to error process), I though I could have the lines instead of a file, in the clipboard.

 

After experimenting a bit I managed to do it as you say cwake.

Command

(setq clipboardtxt (strcat clipboardtxt line "\n"))

did the trick for me.

 

Thank you guys for your help.

You might want to optimize that a bit. Since AutoLisp uses a immutable string (same as DotNet does) it might become slow when you've got 100's or 1000's of lines to concatenate together. This is because every time you concatenate a new string variable is actually created - the old one (or two) are never changed (the very definition of immutable).

 

There are 2 ways you can go about this: Add each line to a list and then use apply to concatenate the entire list into one string in one go, but then since read-line throws away the "\n" character at the end of each line your concatenated string is then all on one single line.

 

Or read the entire file into a string in one go, I'd go with the VBScript library's FileSystemObject and read the entire file into a string object. Like I did in this post: http://www.theswamp.org/index.php?topic=43151.msg483767#msg483767

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...