vb代码让cmd执行命令_如何使用VBScirpt在已打开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)

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

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

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.

Operating System - Windows XP Professional

Thank you!

解决方案

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

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.

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

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

/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

which all specify the WM_CHAR message.

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

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

and h

/wparam:104

and so on.

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: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

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

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

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值