Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy command window to file programmatically? 1

Status
Not open for further replies.

Stella740pl

Programmer
Jul 3, 2003
2,657
US

Is there a simple way to copy contents of the command window to a file programmatically? I would like to include it in the program I run to save the session for later use, along with things like CREATE VIEW and SAVE TO.

Thanks.

Stella
 
stella740pl

What version of VFP are you using? In VFP7.0 everything you write in the command window is actualy kept in a file. (C:\Program Files\Microsoft Visual FoxPro 7\_command.prg)

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
In VFP 7.0 & 8.0, the Command Window history is saved in in a file _Command.PRG. In earlier versions, just make sure the Command Window is active, do a Ctrl+A (highlight All), then Ctrl+C (Copy Selected), then type MODI COMM saveit, then CTRL+V (Paste) into this .PRG. Close and save it!

Rick
 
I use VFP6 SP5.

In earlier versions, just make sure the Command Window is active, do a Ctrl+A (highlight All), then Ctrl+C (Copy Selected), then type MODI COMM saveit, then CTRL+V (Paste) into this .PRG.

That's what I actually always do. I was just wondering if this could be done programmatically.
 
Well, that's what I've already done.
In program file I put the following code:

Activate Window Command
Keyboard '{CTRL+A}'
&& works so far
Keyboard '{CTRL+C}' && doesn't work

Shouldn't it place the selection to the clipboard? It didn't. What did I do wrong? And how to place the contents of the _ClipText to file? It wouldn't fit into a string. Should I use some memo functions and go line by line? Or there is a better way?

Thanks. Stella


 
i use vfp6 sp5 in win98se and so far this worked for me:

Activate Window Command
Keyboard '{CTRL+A}'
Keyboard '{CTRL+C}'
sComHist = _cliptext
strtofile(sComHist, "c:\sCommHist.txt", .t.)


kilroy [trooper]
philippines
"and that's what we call creativity..."
 
kilroy,

Thanks for replying. I tried something like that, and then I tried exactly as you put it, with the same result. It works, except the {CTRL+C} part: the program writes to the file what was in the clipboard before I ran the program, not the contents of the Command Window. I don't know why it happens. I use WinXP Prof, but I am not sure it makes the difference.

Stella
 
stella740pl,

It's a few more steps but may fix your problem (though I am not convinced this is the best way to do this, I think there is probably an easier way and this seems a bit of a hack to me, but as long as it works for you for now):


Activate Window Command && Make sure the command window is the active window
KEYBOARD '{PGDN}' && Go to the bottom to make sure no text is selected
*!* Set cursor focus to the command window
KEYBOARD '{ENTER}' && this will execute code that is selected, so the PGDN was necessary
Keyboard '{CTRL+A}' && Select all
Keyboard '{CTRL+C}' && Copy selected text to clipboard
sComHist = _cliptext &&Get clipboard contents
=strtofile(sComHist, "c:\sCommHist.txt", .t.) && Write variable contents to file


Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
Slighthaze,

Thanks for the ideas. I've just tried your code. Unfortunately, it didn't fix the problem. I can see the text in the command window getting selected (with or without the additional steps), but then I MODI COMM the file and see a clip from an e-mail I copied 5 minutes ago into my text document. I even added a MessageBox() at the end to see that it actually went through all the steps. Still the same.

I kind of suspected that it's not the best way (that's why I posted the question), but it was first that came to mind - to just duplicate what I do manually anyway. So I would be interested to hear what you consider an easier way.

Thanks in advance.
Stella
 
Hi Stella,

Try inserting a doevents for each keyboard command.

Activate Window Command
Keyboard '{CTRL+A}'
DOEVENTS
Keyboard '{CTRL+C}'
DOEVENTS
sComHist = _cliptext
strtofile(sComHist, "c:\sCommHist.txt", .t.)


-- AirCon --
 
Thanks, AirCon.

But still no luck.

I just can't understand, why {CTRL+A} works, and {CTRL+C} doesn't. What's the difference?
 
I'm not sure what is the real problem. [3eyes]

Ok I have tried this (VFP6) and it works. Hope this work for you too.

Activate Window Command
Keyboard '{CTRL+A}'
DOEVENTS
Keyboard '{CTRL+C}'
Keyboard '{CTRL+C}' && Forcing the data to clipboard
DOEVENTS
sComHist = _cliptext
strtofile(sComHist, "c:\sCommHist.txt")


-- AirCon --
 
Well, after some experimenting with different combinations, it looks now like it consistently places the contents of the Command Window to the clipboard. Here's what worked:

ACTIVATE WINDOW Command
_CLIPTEXT=''
KEYBOARD '{CTRL+A}'
DOEVENTS
KEYBOARD '{CTRL+C}'

Here's what didn't:

sComHist = _CLIPTEXT
StrToFile(sComHist, "c:\sCommHist.txt", .f.)

When I assign sComHist=REPLICATE('ABCDE',100000) instead of _CLIPTEXT, it ends up in the file. The contents of the Command Window doesn't (I get an empty file), although I have no problem pasting it to MS Word after the program finished. Something wrong with the _CLIPTEXT?

Stella


 
Stella,

I think there is something ( timing or pending events ?? ) in the process of copying data to clipboard. I'm not really sure about this either.

Try this code. I think it really works now. Watch for the counter:

*--------------------
Declare Long OpenClipboard in User32 Long nhWnd
Declare Long CloseClipboard in User32
Declare Long EmptyClipboard in User32
Declare Short IsClipboardFormatAvailable in User32 ;
Integer nFormat

Local lnCounter

OpenClipboard( 0 )
EmptyClipboard()
CloseClipboard()

Activate window Command
Keyboard '{Ctrl+A}'
DoEvents

lnCounter = 0
Do while (IsClipboardFormatAvailable(1) == 0) and ;
(lastkey() != 27) && Escape key to exit loop

Keyboard '{Ctrl+C}'
DoEvents
lnCounter = lnCounter + 1
Wait alltrim(str(lnCounter)) window nowait
= inkey(0.05, 'HM')

enddo

Set safety off
= StrToFile(_ClipText, 'History.txt', .F.)
Set safety on
Modify file History
*--------------------


-- AirCon --
 
Stella,

I think I was focusing in the wrong area. The KEYBOARD '{CTRL+C}' is not working consistently with VFP Command Editor (I have no idea why :-D )

So I experiment with another approach using FoxApi editor (FoxTools library). And get a consistent behaviour so far. Here it is:


*------------------

Set library to home() + 'foxtools'
Activate window Command
lnWHnd = _wontop()

Keyboard '{Ctrl+A}'
DoEvents
_EdCopy(lnWHnd)

= StrToFile(_ClipText, 'History.txt')

Modify file History

*------------------


-- AirCon --
 
Thanks, AirCon! Wow!

Just last night I realized 4 things:

* Well, yes, the KEYBOARD '{CTRL+C}' is not working consistently with VFP Command Editor - once in a while I got a correct result, but more often I didn't, and I just couldn't figure out, what was in common and what was different in all those cases.

* The contents of the keyboard was placed in the keyboard when DoEvents was issued right before KEYBOARD '{CTRL+C}', but for some reson it was bypassing the _ClipText variable, and I might need to look for ways to either force it to _ClipText, or to get the contents of the clipboard without using the _ClipText.

* The contents of the Command Window is saved in a file in VFP6, too, and this file is located in the temporary directory, although information there doesn't look as good as if it is saved in program file. I was actually considering goind down that route, but there was still a task of figuring out programmatically which file to pick. I am not sure, what 04TO0008.TMP or I6D8009W.TMP exactly mean (system date+time, I guess?), and I usually have 2 instances of VFP open every day, and one stays open for up to 5 days, for long-term projects going on, another can start and close every day or several times a day, for short user data requests or to try something out, like a Tek-Tip, and it would be hard to figure out which of the .tmp files I have to open.

* I felt that I was probably going in the wrong direction, but was not sure which direction is right.

You've done it, it works all the time (actually, both of your pieces are!) and it's short and nice. Thanks! Now, I will have to do some reading on FoxTools, to know better what's out there before I need it again.

Stella.


 
You've done it, it works all the time (actually, both of your pieces are!) and it's short and nice

Yes I know. The first solution also works all the time. It's just that I don't like the solution. I mean it has to force the data to clipboard within DO..WHILE !!

Till, I realize that we were focusing in the wrong one and come up with a better solution (and simpler)


Now, I will have to do some reading on FoxTools, to know better what's out there before I need it again

Happy Coding and Good Luck!
[2thumbsup]


-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top