Page 1 of 1

Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 05 Oct 2020, 09:34
by Albireo
Hi!
Sending a result from a CMD prompt (eg dir) to a temporary file (c:\temp\out.txt) is not difficult. (something like this)

Code: Select all

outFile := "c:\temp\out.txt"
If FileExist(outFile)
	FileDelete %OutFile%
 
RunWait % ComSpec " /c dir c:\temp\*.* > " outFile,, Hide

FileRead DirTree, %outFile%
FileRecycle %outFile%
MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, Ready! `n`n%DirTree%
The only problem I see with this is that the file does not have the correct Code Page - for me.
17 File(s) 729ÿ775ÿ009 bytes (ÿ instead of "space")
But is it possible (in some way) to send the result directly to an variable och Clipboard from the CMD-window?
Something like this .: (don't work)

Code: Select all

Clipboard := ""
RunWait % ComSpec " /c dir c:\temp\*.* > " Clipboard,, Hide
MsgBox % ClipBoard
(maybe better to save Clipboard before CMD/dir and restore after CMD/dir?)

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?  Topic is solved

Posted: 05 Oct 2020, 11:03
by Xtra
Try:

Code: Select all

RunWait % ComSpec " /c dir |Clip", C:\Temp, Hide

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 05 Oct 2020, 11:33
by MrDoge
I forgot where I found this (in the forums, not mine)

Code: Select all

output:=ComObjCreate("WScript.Shell").Exec("Dir").StdOut.ReadAll()
clipboard:=output
msgbox % output

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 05 Oct 2020, 13:09
by Albireo
Thanks for the suggestions!
MrDoge wrote:
05 Oct 2020, 11:33
...

Code: Select all

output:=ComObjCreate("WScript.Shell").Exec("Dir").StdOut.ReadAll()
clipboard:=output
msgbox % output
An old solution from Lexikos, among others (but I got an error message)
Error: 0x80070002 - The file can not be found.
Source: WshShell.Exec


Xtra wrote:
05 Oct 2020, 11:03

Code: Select all

RunWait % ComSpec " /c dir |Clip", C:\Temp, Hide
This solution seems to work.
But I can not find any information about Clip (is it a type of variable?)

Since it is not the Dir instruction but certutil.exe that I would handle, the program will be different.
This is an example of my solution .:

Code: Select all

FileSelectFile SelectedFile, 3, c:\Temp, Open a file, Text Documents (*.jpg)
If !FileExist(SelectedFile)
{	MsgBox 16, %A_ScriptName% - Row %A_LineNumber%, You didn't select anything.
	ExitApp
}

md5value := md5(SelectedFile)

MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % "MD 5`n`n- " md5value "`n`n- " SelectedFile
ExitApp

md5(file)
{	If !FileExist(file)
		Return

	RunWait % ComSpec " /c " A_WinDir "\System32\certutil.exe -hashfile """ file """ MD5 | Clip" ,, Hide	
	Loop Parse, Clipboard, `n, `r
	{	If ( A_Index = 2 )
		{	line := A_LoopField
			Break
		}
	}
	Return line
}

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 05 Oct 2020, 14:08
by Xtra

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 05 Oct 2020, 20:12
by RickC
Clip refers to C:\Windows\System32\clip.exe. As the filepath is already referenced by the PATH environment variable you only need to use the command clip itself to refer to the command and its filepath.

Hope this helps...

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 09 Oct 2020, 12:37
by A_AhkUser
Albireo wrote:
05 Oct 2020, 13:09
Thanks for the suggestions!
MrDoge wrote:
05 Oct 2020, 11:33
...

Code: Select all

output:=ComObjCreate("WScript.Shell").Exec("Dir").StdOut.ReadAll()
(...) (but I got an error message)
Error: 0x80070002 - The file can not be found.
Source: WshShell.Exec

For reference:

Code: Select all

; https://www.vbsedit.com/html/f3358e96-3d5a-46c2-b43b-3107e586736e.asp
WshRunning := 0 ; WshFinished := 1
WshShell  := ComObjCreate("WScript.Shell")
oExec := WshShell.Exec("cmd /c DIR """ . A_Desktop . """")
#Persistent
SetTimer, wait, -1
return

wait:
	if (oExec.Status = WshRunning)
		SetTimer,, -10
	else {
		clipboard := oExec.StdOut.ReadAll()
	ExitApp
	}
return
A_AhkUser

Re: Use Clipboard instead of temp-file from CMD-prompt - Possible?

Posted: 09 Oct 2020, 14:34
by Albireo
Thank you! @A_AhkUser
(sorry I do not understand)
What is supposed to happen?
How is this used?