CMD/ComObject: 为什么这个 CMD window 的内容为零?
CMD/ComObject: why this CMD window has zero content?
我正在编写一个自动热键脚本,其中 运行 是一些 CMD 命令。考虑到其他因素,最后只好用以前没学过的ComObject来做。但归根结底,这个问题是关于CMD命令的。
abc是一个3行的可变字符串,代表3条命令。此代码将 运行 这 3 个命令然后退出。
请看一下:原来是"exec := shell.Exec(ComSpec " /Q /K echo off")",所以cmd window完全隐藏了,所以我改成" /K echo on ",现在我可以看到带有标题的 CMD window 和所有 3 个命令 运行 成功,但在整个过程中,此 CMD window 中没有文本。我该如何解决?
abc := "xxxxxx"
RunWaitMany(abc)
RunWaitMany(commands) {
shell := ComObjCreate("WScript.Shell")
; Open cmd.exe with echoing of commands disabled
exec := shell.Exec(ComSpec " /K echo on")
; Send the commands to execute, separated by newline
exec.StdIn.WriteLine(commands "`nexit") ; Always exit at the end!
; Read and return the output of all commands
return exec.StdOut.ReadAll()
}
那么,我认为您想要做的是让 cmd 与您的命令输出一起出现?
您正在使用的 AHK 文档中的示例函数仅用于读取命令的输出,并且它成功地做到了这一点。
我想这是你想要的:
Run, % A_ComSpec " /K echo Hello!"
(Run, %ComSpec% /K echo Hello!
在传统 AHK 中)
为了将更多命令链接在一起,您可以将它们包装在 "
中并使用 &
(我不是 cmd 专家,所以不确定 &
是否可以在每个案例,但是是的):
Run, % A_ComSpec "
(Join
/K ""echo Hi, lets list the IPv4 addresses in the ipconfig command.
& ipconfig | findstr IPv4
& echo Sweet, it worked
& echo Bye!""
)"
A continuation section 用于将其分成多行并更具可读性。
同样的事情在这里没有延续部分:
Run, % A_ComSpec " /K ""echo Hi, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet, it worked & echo Bye!"""
或者在传统 AHK 中:
Run, %ComSpec% /K "echo Hi`, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet`, it worked & echo Bye!"
如果把它作为一个函数很重要,像这样的东西就可以了
MyCommands := "
(Join`n
echo Hi, lets list the IPv4 addresses in the ipconfig command.
ipconfig | findstr IPv4
echo Sweet, it worked
echo Bye!
)"
RunCommands(MyCommands)
return
RunCommands(commands)
{
Run, % A_ComSpec " /K """ RegExReplace(commands, "`r?`n", "&") """"
}
因此用 &
替换换行符。
我正在编写一个自动热键脚本,其中 运行 是一些 CMD 命令。考虑到其他因素,最后只好用以前没学过的ComObject来做。但归根结底,这个问题是关于CMD命令的。
abc是一个3行的可变字符串,代表3条命令。此代码将 运行 这 3 个命令然后退出。 请看一下:原来是"exec := shell.Exec(ComSpec " /Q /K echo off")",所以cmd window完全隐藏了,所以我改成" /K echo on ",现在我可以看到带有标题的 CMD window 和所有 3 个命令 运行 成功,但在整个过程中,此 CMD window 中没有文本。我该如何解决?
abc := "xxxxxx"
RunWaitMany(abc)
RunWaitMany(commands) {
shell := ComObjCreate("WScript.Shell")
; Open cmd.exe with echoing of commands disabled
exec := shell.Exec(ComSpec " /K echo on")
; Send the commands to execute, separated by newline
exec.StdIn.WriteLine(commands "`nexit") ; Always exit at the end!
; Read and return the output of all commands
return exec.StdOut.ReadAll()
}
那么,我认为您想要做的是让 cmd 与您的命令输出一起出现?
您正在使用的 AHK 文档中的示例函数仅用于读取命令的输出,并且它成功地做到了这一点。
我想这是你想要的:
Run, % A_ComSpec " /K echo Hello!"
(Run, %ComSpec% /K echo Hello!
在传统 AHK 中)
为了将更多命令链接在一起,您可以将它们包装在 "
中并使用 &
(我不是 cmd 专家,所以不确定 &
是否可以在每个案例,但是是的):
Run, % A_ComSpec "
(Join
/K ""echo Hi, lets list the IPv4 addresses in the ipconfig command.
& ipconfig | findstr IPv4
& echo Sweet, it worked
& echo Bye!""
)"
A continuation section 用于将其分成多行并更具可读性。
同样的事情在这里没有延续部分:
Run, % A_ComSpec " /K ""echo Hi, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet, it worked & echo Bye!"""
或者在传统 AHK 中:
Run, %ComSpec% /K "echo Hi`, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet`, it worked & echo Bye!"
如果把它作为一个函数很重要,像这样的东西就可以了
MyCommands := "
(Join`n
echo Hi, lets list the IPv4 addresses in the ipconfig command.
ipconfig | findstr IPv4
echo Sweet, it worked
echo Bye!
)"
RunCommands(MyCommands)
return
RunCommands(commands)
{
Run, % A_ComSpec " /K """ RegExReplace(commands, "`r?`n", "&") """"
}
因此用 &
替换换行符。