.vbs脚本将无法运行批处理/如何以静默方式运行批处理 [英] .vbs script won't run batch / how to run batch silently
问题描述
我有,我想静静地运行批处理文件。
所以,我goolging四周,拿出以下code ...
invMybatchfile.vbs
I have a batch file that I want to run silently. So, i was goolging around and come up with following code... invMybatchfile.vbs
Dim curPath
curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, True)
这code我的台式机(微软Windows 7 32位)上工作,但由于某种原因,这个脚本并不服务器计算机(在Windows Server 2008 R2)上工作。当我点击invMybatchfile.vbs,DOS窗口弹出和关闭马上并且完全不执行Mybatchfile.bat。 (如果我只是在服务器计算机上运行Mybathfile.bat,它做工精细,所以批处理文件是不是这里的问题。)
This code DOES work on my desktop (Window 7 32bit), but for some reason, this script does not work on server computer(Windows Server 2008 R2). When I click invMybatchfile.vbs, dos windows pops up and closes right away and Mybatchfile.bat is not executed at all. (If i just run Mybathfile.bat on server computer, it works fine, so batch file is not the problem here.)
所以我的问题是,没有人知道为什么我有这个问题?
So my question is, does anyone know why I am having this problem?
或
有没有其他的方式来默默启动批处理文件(没有最小化,我知道这种方式)使用.vbs文件或安装其它软件呢?
is there any other ways to launch batch file silently(not minimized, i know this way) with out using .vbs file or installing other software?
您的帮助谢谢
JB
Thanks for your help JB
推荐答案
您例子的code,因为你在声明变量名称为curPath永远不会在任何机器上运行,并指定vlaue为CurPath但是你要使用它的名称为sCurPath(这变量上的code不存在)。
The code of your example never will run in any machine because you are declaring the variable name as "curPath" and assign the vlaue to "CurPath" but you are trying to use it with the name "sCurPath" (And that variable doesn't exist on your code).
您也不必设置当前工作目录,因为当你启动的文件,外壳搜寻SEARCH_TERM_EXAMPLES在工作目录文件,所以这是所有code你需要什么,在只有一条线
Also you don't need to set the current working dir because when you launch a file, the shell searchs for the file in the working dir, so this is all the code what you need, in only one line:
CreateObject("WScript.Shell").RUN "Mybatchfile.bat", 0, True
但是,如果你有兴趣存储或使用当前的工作目录必须返回当前目录的方法任何奇怪的原因:
But if you are interested to store or to use the current working directory for any strange reason you have a method that returns the current dir:
.CurrentDirectory
然后你可以使用的方法是这样的:
Then you can use the method this way:
Set Shell = CreateObject("WScript.Shell")
Shell.RUN Shell.CurrentDirectory & "\Mybatchfile.bat", 0, True
...或者当前目录这种方式存储在一个VAR:
...Or store the current dir in a var this way:
Set Shell = CreateObject("WScript.Shell")
CurDir = Shell.CurrentDirectory & "\"
Shell.RUN CurDir & "Mybatchfile.bat", 0, True
MSDN : http://msdn.microsoft.com/en-us/library/3cc5edzd%28v=vs.84%29.aspx
编辑:
关于运行无声文件,可以使用过的的NirCmd 命令行应用程序做到这一点。
About running silent files, you can do it too using NirCMD commandline application.
NirCMD exec hide "Path to Batch File\Batch File.bat"
官方网站: http://www.nirsoft.net/ utils的/ nircmd2.html
这篇关于.vbs脚本将无法运行批处理/如何以静默方式运行批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!