如何使用VBScirpt在已打开CMD窗口执行命令 [英] How to execute a command in already open CMD window using VBScirpt

查看:808
本文介绍了如何使用VBScirpt在已打开CMD窗口执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动执行,必须每天做备份。我有一个CMD程序运行,这需要在运行备份之前正确关闭。
它需要关闭的两个命令是:
关闭
是(以确认动作)

I am trying to automate backups that have to be done daily. I have a program running in CMD, which needs to be properly closed before running the backup. The two commands that it needs to close are: shutdown yes (to acknowledge the action)

然后就是CMD窗口可以关闭,我已经有.bat文件写入到执行备份。

Then that CMD window can be closed, and I already have .bat file written to execute a backup.

我从来没有写过VB脚本只是很简单的人,所以我很抱歉,如果我的问题不够清楚。

I have never written VBScripts except very very simple ones, so I am sorry if my question is not clear enough.

的想法是这样的 - 使用VBScript,类型都关机{输入}没错{回车},然后关闭CMD窗口,然后执行备份的批处理文件,它已经被设置为启动CMD程序一旦完成备份。

The idea is this - using VBScript, type in both shutdown {Enter} and yes {Enter}, then close that CMD window, and then execute backup batch file, which already is set to start the CMD program once it is done backing up.

操作系统 - Windows XP专业版

Operating System - Windows XP Professional

感谢您!

推荐答案

您可以尝试在命令发送击键作为Windows消息的CMD窗口,在程序运行编程输入。

You could try typing in the commands programmatically by sending keystrokes as Windows messages to the CMD window where your program is running.

我不知道VBS,也许它有一个或多个函数映射到系统API函数的 PostMessage的 和/或的 SendMessage函数 。将是巨大的,如果它没有,特别是如果有人张贴一个答案解释如何使用它们。在此期间,我可以告诉你如何使用这种方法具有的工具的所谓的 SendMessage函数,在一个批处理文件。

I don't know VBS, perhaps it has a function or functions mapping to the system API functions PostMessage and/or SendMessage. Would be great if it did, especially if someone posted an answer explaining how to use them. In the meantime, I can show you how to use this method with the help of a utility called SendMessage, in a batch file.

基本上,该工具允许你发送任意的Windows消息,像这样的任意窗口:

Basically, the utility allows you to send an arbitrary Windows message to an arbitrary window like this:

SendMessage.exe target_specification /message:value /wparam:value /lparam:value 

我会解决的 target_specification 的咬了一下后,和其他的参数是:

I'll be addressing the target_specification bit a bit later, and the other parameters are:


  • /消息: - 正在发送的消息,通过的 $ C $指定ç 的。在你情况下,将以下任一:

  • /message:value – the message being sent, specified by code. In you case it would be either of these:

/message:WM_CHAR
/message:258
/message:0x0102

所有指定的 WM_CHAR消息

/ WPARAM: - 为WM_CHAR消息,这一项应该击键的code发送

/wparam:value – for the WM_CHAR message, this one should be the code of the keystroke being sent.

在你的情况,因为你的命令只能由ASCII字符组成,所有的codeS将匹配ASCII codeS对应的字符。因此,您可以使用 ASCII图表的字符转换在你的命令。在取值,例如将

In your case, since your commands consist of ASCII characters only, all the codes would match the ASCII codes of the corresponding characters. Therefore, you can use an ASCII chart to convert the characters in your commands. The s, for instance would be

/wparam:115

^ h

/wparam:104

等。

请注意,您还需要发送<大骨节病>输入键presses了。按照 ASCII控制$ C $ ç图,这将是

Note that you will also need to be sending the Enter key presses too. According to the ASCII control code chart, it would be

/wparam:13


  • / LPARAM: - 你可以从的 WM_CHAR手册页面的lParam参数是实际上应该携带多条信息作为一个单一的数值。但是,对于您的特定情况下,它是不够的,只是记住,这个参数应该只是作为

  • /lparam:value – you can see from the WM_CHAR's manual page that the LPARAM parameter is actually supposed to carry multiple pieces of information as a single numeric value. However, for your specific case it would be enough to just memorise that this parameter should be specified simply as

    /lparam:1
    


  • 现在到目标规范的一部分。这其中可能会非常棘手。你会从描述学习在工具的网页了有指定目标窗口的不同方式。并不是每一个方法可以为你工作,虽然。

    Now to the target specification part. This one may be tricky. You will learn from the description at the tool's web page that there are different ways of specifying the target window. Not every method may work for you, though.

    例如,你可以尝试进程名指定目标:

    For instance, you could try specifying the target by the process name:

    /processname:name_of_your_executable
    

    不过,如果你不直接调用你的程序,而是在例如叫它一个批处理文件,则该程序将不会在自己的窗口中运行,但会宁愿被借用托管CMD会话的窗口,所以这种方法可能会无法正常工作。

    However, if you are not invoking your program directly and instead are calling it in e.g. a batch file, then the program will not be running in its own window but will rather be borrowing the hosting CMD session's window, and so this method will likely not work.

    如果这确实是你如何调用该程序,那么你可以尝试使用窗口标题来指定目标:

    If that is indeed how you invoke the program, then you could try using the window title to specify the target:

    /windowtitle:window_title
    

    要记住这里唯一需要注意的是,标题必须唯一地标识所需的窗口,或者击键将被发送到匹配指定标题的所有窗口。你可以尝试使用标题在调用程序批命令(设置它在调用程序)uniquifying称号。

    The only caveat to keep in mind here is that the title must uniquely identify the required window, or the keystrokes would be sent to all the windows matching the specified title. You could try uniquifying the title by using the title command in the batch that calls the program (setting it prior to calling the program).

    所以,概括起来讲,这里是如何完整的备份脚本(批量的脚本)可能看起来像,假设,对于这个例子的目的,在目标窗口将其标题指定

    So, to sum it up, here's how the complete backup script (batch script) might look like, assuming, for the purpose of the example, that the target window will be specified by its title:

    @ECHO OFF
    :: s
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:115 /lparam:1
    :: h
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:104 /lparam:1
    :: u
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:117 /lparam:1
    :: t
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:116 /lparam:1
    :: d
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:100 /lparam:1
    :: o
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:111 /lparam:1
    :: w
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:119 /lparam:1
    :: o
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:110 /lparam:1
    :: ENTER
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:13 /lparam:1
    
    :: y
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:121 /lparam:1
    :: e
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:101 /lparam:1
    :: s
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:115 /lparam:1
    :: ENTER
    SendMessage.exe /windowtitle:"My Title" /message:WM_CHAR /wparam:13 /lparam:1
    
    :: wait for some time (~3 sec) till the program shuts down, if necessary
    PING -n 4 localhost 1>NUL
    
    :: proceed with the back-up
    ...
    

    这篇关于如何使用VBScirpt在已打开CMD窗口执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆