RunCMD() v0.97 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

07 Jun 2021, 17:03

blue83 wrote:
07 Jun 2021, 16:46
Hi,

Can you help me.

This is working fine

Code: Select all

avar := RunCmd("" A_ScriptDir "\Upload.exe", "" A_ScriptDir "\" folder1 "", "UTF-8")
but this code with 2 subfolders does not

Code: Select all

avar := RunCmd("" A_ScriptDir "\Upload.exe", "" A_ScriptDir "\" folder1 "\" folder2 "", "UTF-8")


Hi @blue83

Can't really say without knowing contents of variables you use.
Did you test if your path is correct?

Code: Select all

MsgBox % ("" A_ScriptDir "\" folder1 "\" folder2 "")
MsgBox % FileExist("" A_ScriptDir "\" folder1 "\" folder2 "")
..and why the leading "" in your path?
blue83
Posts: 157
Joined: 11 Apr 2018, 06:38

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

08 Jun 2021, 00:53

Hi @SKAN ,

Thank you for your help.

Acctually all the time I was retyping my code and your function, but finally problem was im my .exe file.

But without you to open my eyes I would still have this problem :)

Thank you!

Blue
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

Re: RunCMD() v0.94

11 Jun 2021, 01:53

SKAN wrote:
30 May 2021, 01:37
Hi Skan, thank you very much! This solved my issue. I ran into another one though that i can't seem to figure out.
I'm trying to send a POST request with a json object. The command works in powershell itself, but the json object gets ignored when i try it through RunCMD.

Code: Select all

token = 1725945266:AAG80jx5H8kMyqC6M_6Vr5wzFfqv8wL1KK4
URL = https://api.telegram.org/bot%token%/getupdates

; supposed to show only the last 2 posts, but i get back all of them.
Command = powershell (Invoke-WebRequest -UseBasicParsing %URL% -ContentType 'application/json' -Method POST -Body '{ "limit":2 }').content
msgbox % RunCMD(Command)

Code: Select all

; This works perfectly when i try it directly in powershell though, i get back 2 posts
(Invoke-WebRequest -UseBasicParsing https://api.telegram.org/bot1725945266:AAG80jx5H8kMyqC6M_6Vr5wzFfqv8wL1KK4/getupdates -ContentType 'application/json' -Method POST -Body '{ "limit":2 }').content

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94

11 Jun 2021, 02:39

Thoughtfu1Tux wrote:
11 Jun 2021, 01:53
I'm trying to send a POST request with a json object. The command works in powershell itself, but the json object gets ignored when i try it through RunCMD.

....

Code: Select all

; This works perfectly when i try it directly in powershell though, i get back 2 posts
(Invoke-WebRequest -UseBasicParsing https://api.telegram.org/bot1725945266:AAG80jx5H8kMyqC6M_6Vr5wzFfqv8wL1KK4/getupdates -ContentType 'application/json' -Method POST -Body '{ "limit":2 }').content

I don't get 2 posts when I Run the CMD through command prompt
If you fix it to work in command prompt then it should work correctly with RunCMD()
 
image.png
image.png (67.18 KiB) Viewed 3993 times
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

07 Aug 2021, 20:51

@SKAN is possible to keep the window running instead of creating a new process each time a command is passed?
Im calling the function so often (200ms) the CPU usage goes crazy.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

07 Aug 2021, 21:44

pv007 wrote:
07 Aug 2021, 20:51
@SKAN is possible to keep the window running instead of creating a new process each time a command is passed?
If that is possible (at all), it has to be implemented as class which I am not comfortable doing right now.
I will look into it.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

10 Aug 2021, 10:07

@SKAN

If you want to collaborate, I'd be happy to try something with you. I like your coding style. You seem to accomplish more in fewer lines. I've been wondering how to make my class lib shorter.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

10 Aug 2021, 10:15

As for CPU usage, i had high cpu usage checking the buffer as fast as possible. If you insert approx a 10 ms delay in the loop that reads the buffer, that should help with CPU usage with minimal slowing (imperceptable I'd say).
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

10 Aug 2021, 10:49

TheArkive wrote:
10 Aug 2021, 10:07
@SKAN
I like your coding style. You seem to accomplish more in fewer lines.
I'm not sure if I prefer to do that anymore. :)
I'm getting old and can't always remember the shortcuts/hacks I introduce in my code.
Short code isn't always the efficient way.
Unless it enhances performance, It isn't worth the trouble, I would say.
TheArkive wrote:
10 Aug 2021, 10:07
I've been wondering how to make my class lib shorter.
It has been 5 years since I wrote a class.
Right now, I am working hard to gain muscle memory in V2.
I feel so noobish :(
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

10 Aug 2021, 10:53

hehe, I know the feeling - brushing off old skills. I'd be happy to share "how I might do something" in a class if you like.

Maybe I can offer something that will help ya brush off the dust ;)
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

10 Aug 2021, 12:45

SKAN wrote:
10 Aug 2021, 10:49
It has been 5 years since I wrote a class.
If it's anything to you, some people might prefer just having the function(s).
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

11 Aug 2021, 03:44

TheArkive wrote:
10 Aug 2021, 10:53
I'd be happy to share "how I might do something" in a class if you like.
I'm creating one massive always-running script and hopefully it will cover 75% of AHK.
Any class would occur probably on the final phase.

Thank you. I''ll ping you when help is needed.
SOTE wrote:
10 Aug 2021, 12:45
some people might prefer just having the function(s).
Some people seem to prefer only class and insist everyone does :(..
I really needed/used it only twice so far and wouldn't hesitate to use again..
.. but only if I don't have a choice.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

11 Aug 2021, 05:58

SKAN wrote:
11 Aug 2021, 03:44
Some people seem to prefer only class and insist everyone does :(..
It's one of those things. Some love using Classes, while others hate it and prefer a more functional/procedural style, only records/structs, or objects without Classes. But, it should be one of those things where it isn't forced unless someone is specifically paying and mandating its use, otherwise it should be a choice. If they love Classes so much, they could always convert the code over to their preference, the same as others who might convert code as a Class to a more functional/procedural style.

Different strokes for different folks.
N_athan
Posts: 40
Joined: 21 Aug 2021, 16:40

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

21 Aug 2021, 21:25

Nice function, is possible to return/cancel after a given time without a response?
Im running some commands and when the server is offline it stuck in the function forever as it doesn't return a response.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

22 Aug 2021, 20:08

From the OP:
During the lifetime of the created process, a global array element A_Args.RunCMD.PID will contain the PID and assigning a 0 (false) to this element will cancel RunCMD().
N_athan
Posts: 40
Joined: 21 Aug 2021, 16:40

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

22 Aug 2021, 20:33

kczx3 wrote:
22 Aug 2021, 20:08
From the OP:
During the lifetime of the created process, a global array element A_Args.RunCMD.PID will contain the PID and assigning a 0 (false) to this element will cancel RunCMD().
Could you give an example? I could not understand.
N_athan
Posts: 40
Joined: 21 Aug 2021, 16:40

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

24 Aug 2021, 21:30

Does someone else have any example of how to cancel after a given timeout?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: RunCMD() v0.94 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

24 Aug 2021, 21:43

N_athan wrote:
24 Aug 2021, 21:30
how to cancel after a given timeout?

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force
SetBatchLines, -1


SetTimer, Cancel, -1000
MsgBox % clipboard := RunCmd(A_Comspec . " /c Dir *.* /s", "C:\") ; list all file in Drive C:

Cancel:
  Process, Close, % A_Args.RunCMD.PID
Return
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: RunCMD() - Code updated

25 Aug 2021, 16:57

SKAN wrote:
13 May 2020, 15:53
You may call A_Args.RunCMD.PID := 0 from a hotkey or routine to exit gracefully... or
call Process, Close, % A_Args.RunCMD.PID to terminate.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 117 guests