返回列表 发帖

[问题求助] [已解决]VBS怎样模拟键盘输入程序?

因为工作的原因,每天早6点就得到单位调数,很累想求助一个批处理程序,实现如下功能:
首先,在前一天下班前运行程序,程序开始自动检测当前的系统时间是否为早晨6:00?不是继续检测。是,运行指定程序(比如:以DOS为例)。
第二功能,打开调数程序(比如:以DOS为例)。
第三功能,DOS窗口打开了,要根据提示录入英文字母、数字、空格、Del键、回车键等。比如:录入qwe122后面要有个回车才能进行下一个提示的录入;还有如,hh空格123.sh同样后面要有个回车才能进行下一个提示;录入了几次上述的条件后,系统会弹出个大菜单,只有按一次Del键才能继续录入像上面例子中的条件。
----依次录入字符串(注:英文小写)qwe2100回车;qqq111回车;按Delete键;sh bbw.sh回车;qqq11100回车;20100101回车;20100228回车;
第四功能,再在桌面上新建一个以当前系统日期为名的文件夹。
我在网上苦找很久,有一个叫vbs的文章模拟键盘输入,介绍的内容好像可以帮我实现第三功能,但我不懂,请高手帮忙看看好吧
--------------------------------------------------------------------------------------------------------
以下是我在网上找的初步能满足上述功能小段落,但是请高手相助:
[b]功能二代码:[/b]
@echo off
echo...正在运行软件……请稍后.
start  C:\PROGRA~1\Dos\Dos.exe 212.1.30.45
[exit]COPY
[b]功能四代码:[/b]@echo off
start mshta vbscript:execute("createobject(""scripting.filesystemobject"").CreateFolder date()")(window.close)
exit
COPY
功能三代码:【原文网址:http://hi.baidu.com/yangbu/blog/ ... 70b4369b5027f0.html
Set objShell = CreateObject("Wscript.Shell")
strCommandLine = "ttpsetup.exe"        '启动安装程序
objShell.Run(strCommandLine)
set WshShell = CreateObject("WScript.Shell")
WScript.Sleep 3000        '延时3秒
WshShell.SendKeys "s"        '模拟按下 S 键开始安装
WScript.Sleep 300        '延时0.3秒
WshShell.SendKeys "a"        '模拟按下 A 键同意协议
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"        '模拟按下回车键执行下一步
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"        '模拟按下回车键执行下一步
WScript.Sleep 300
WshShell.SendKeys "{ENTER}"        '模拟按下回车键执行下一步
WScript.Sleep 2000
WshShell.SendKeys "{TAB}"        '模拟按下 Tab 键选择安装项目
WScript.Sleep 300
WshShell.SendKeys "{TAB}"
WScript.Sleep 300
WshShell.SendKeys " "        '模拟按下空格键去除安装百度超级搜霸一行前的√号
WScript.Sleep 300
[WshShell.SendKeys "{ENTER}"        '模拟按下回车键完成最后一步COPY
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 1# dawei124578


    嗯,先提供个思路:
不用检测是否为6:00,用 at命令计划任务,然后用VBS运行程序和模拟按键。现在不在电脑旁边,晚上我回去再看看
---学无止境---

TOP

记住最后那部分发送模拟按键的,根据你的需要自己修改可以了,其他功能都OK了
@rem 保存我为 AutoWork.bat
@echo off
if not exist Work.vbs more +6 "%~f0">Work.vbs
at 6:00 /interactive "Work.vbs"
exit
Dim wsh,strPath,fso
Set fso = CreateObject("Scripting.FileSystemObject")
Rem 下面是程序的路径
strPath = "C:\Dos.exe"
set wsh = CreateObject("WScript.Shell")
fso.CreateFolder wsh.SpecialFolders("Desktop") & "\" _
& DatePart("yyyy",Date) & "-" _
& DatePart("m",Date) & "-" _
& DatePart("d",Date)
wsh.Run strPath
WScript.Sleep 3000 '延时3
wsh.SendKeys "test"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "test"
WScript.Sleep 500
wsh.SendKeys "{enter}"
Rem 重复用上面的步骤直到输入完你要输入的值COPY
---学无止境---

TOP

感谢版主的帮助,小弟有些小问题请版主相告:
1、我把这段代码复制到名为 文本文档.txt里,
2、是不是从第10行和第18行开始改成自己实际路径。您看是这样吗?
@rem 保存我为 AutoWork.bat
@echo off
if not exist Work.vbs more +6 "%~f0">Work.vbs
at 6:00 /interactive "Work.vbs"
exit
Dim wsh,strPath,fso
Set fso = CreateObject("Scripting.FileSystemObject")
Rem 下面是程序的路径
strPath = "C:\PROGRA~1\Dos\Dos.exe 212.1.30.45"
set wsh = CreateObject("WScript.Shell")
fso.CreateFolder wsh.SpecialFolders("Desktop") & "\" _
        & DatePart("yyyy",Date) & "-" _
        & DatePart("m",Date) & "-" _
        & DatePart("d",Date)
wsh.Run strPath
WScript.Sleep 3000 '延时3
wsh.SendKeys "qwe2100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq111"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "DELETE"
WScript.Sleep 500
wsh.SendKeys "sh bbw.sh"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq11100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100101"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100228"
WScript.Sleep 500
wsh.SendKeys "{enter}"
Rem 重复用上面的步骤直到输入完你要输入的值COPY
3、@rem 保存我为 AutoWork.bat,是啥意思?是说要将文本文档.txt的名和扩展名改成autowork.bat吗?
4、运行这个批处理后,在桌面上自动生成了一个Work.vbs。但是并没有生成文件夹。我只有双击work.vbs后,才会出现文件夹和打开DOS,并录入了相应命令。这都是靠人工打开才有的呀?

TOP

本帖最后由 broly 于 2011-8-28 12:59 编辑

回复 4# dawei124578


    嗯,是的,现在你目前的操作都是对的。还有,那个VBS不用你手动去打开,我用批处理加了计划任务,到6:00会自己打开的了(当然,前提是你开机了)。你只是下班前打开批处理文件就可以了

还有你的代码有点错误,就是  delete 键。这些特殊键要加个括号  {delete}

参考:http://baike.baidu.com/view/1133305.htm
@rem 保存我为 AutoWork.bat
@echo off
more +6 "%~f0">Work.vbs
at 6:00 /interactive "Work.vbs"
exit
Dim wsh,strPath,fso
Set fso = CreateObject("Scripting.FileSystemObject")
Rem 下面是程序的路径
strPath = "C:\PROGRA~1\Dos\Dos.exe 212.1.30.45"
set wsh = CreateObject("WScript.Shell")
fso.CreateFolder wsh.SpecialFolders("Desktop") & "\" _
        & DatePart("yyyy",Date) & "-" _
        & DatePart("m",Date) & "-" _
        & DatePart("d",Date)
wsh.Run strPath
WScript.Sleep 3000 '延时3秒
wsh.SendKeys "qwe2100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq111"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "{DELETE}"
WScript.Sleep 500
wsh.SendKeys "sh bbw.sh"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq11100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100101"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100228"
WScript.Sleep 500
wsh.SendKeys "{enter}"
Rem 重复用上面的步骤直到输入完你要输入的值COPY
---学无止境---

TOP

???我的电脑为什么双击完批处理后,等右下角时间到6:00后,并没有自动运行

TOP

你什么时候按的?你指的6:00是早上的吗?
---学无止境---

TOP

回复 7# broly


    是早上6点,

TOP

回复 8# dawei124578


    你现在运行一次批处理,然后在命令提示符下输入 at ,然后回车,看看里面的内容是什么
---学无止境---

TOP

回复 9# broly


    一运行,窗口一闪就没了

TOP

回复 10# dawei124578


    呵呵,我说呢,原来你的  at 服务都还没开通。解决方法:控制面板->管理工具->服务->开启Task Scheduler服务
黑框闪一下就没了是正常的,因为运行完了就退出了。
---学无止境---

TOP

回复 11# broly


    是这样吗?

TOP

回复 12# dawei124578


    你重复9楼的步骤看看成功了没
---学无止境---

TOP

回复 12# dawei124578


    第一步:重启电脑,运行命令提示符:at,如下图1
[attach]4356[/attach]

    第二步:在管理工具--服务---把task scheduler状态改为启动,如下图2
[attach]4357[/attach]

   第三步:在29日,5:57:36运行了批处理,如下图3
[attach]4358[/attach]

   第四步:再次运行命令提示符:at,如下图4
[attach]4359[/attach]

   第五步:等待6:00后批处理能自动打开程序,并录入设置好的命令字符,但是奇迹没有出现。如下图5
@rem 保存我为 AutoWork.bat
@echo off
if not exist Work.vbs more +6 "%~f0">Work.vbs
at 6:00 /interactive "Work.vbs"
exit
Dim wsh,strPath,fso
Set fso = CreateObject("Scripting.FileSystemObject")
Rem 下面是程序的路径
strPath = "C:\112.txt"
set wsh = CreateObject("WScript.Shell")
fso.CreateFolder wsh.SpecialFolders("Desktop") & "\" _
        & DatePart("yyyy",Date) & "-" _
        & DatePart("m",Date) & "-" _
        & DatePart("d",Date)
wsh.Run strPath
WScript.Sleep 3000 '延时3
wsh.SendKeys "qwe2100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq111"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "{DELETE}"
WScript.Sleep 500
wsh.SendKeys "sh bbw.sh"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq11100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100101"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100228"
WScript.Sleep 500
wsh.SendKeys "{enter}"
Rem 重复用上面的步骤直到输入完你要输入的值COPY
[attach]4360[/attach]

TOP

呵呵,sorry,有个地方弄错了,现在你试试下面的代码
@rem 保存我为 AutoWork.bat
@echo off
more +6 "%~f0">Work.vbs
at 6:00 /interactive "%~dp0Work.vbs"
exit
Dim wsh,strPath,fso
Set fso = CreateObject("Scripting.FileSystemObject")
Rem 下面是程序的路径
strPath = "C:\112.txt"
set wsh = CreateObject("WScript.Shell")
fso.CreateFolder wsh.SpecialFolders("Desktop") & "\" _
        & DatePart("yyyy",Date) & "-" _
        & DatePart("m",Date) & "-" _
        & DatePart("d",Date)
wsh.Run strPath
WScript.Sleep 3000 '延时3秒
wsh.SendKeys "qwe2100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq111"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "{DELETE}"
WScript.Sleep 500
wsh.SendKeys "sh bbw.sh"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "qqq11100"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100101"
WScript.Sleep 500
wsh.SendKeys "{enter}"
WScript.Sleep 500
wsh.SendKeys "20100228"
WScript.Sleep 500
wsh.SendKeys "{enter}"
Rem 重复用上面的步骤直到输入完你要输入的值COPY
2

评分人数

---学无止境---

TOP

返回列表