简体   繁体   中英

.vbs script won't run batch / how to run batch silently

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)

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?

OR

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?

Thanks for your help JB

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).

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

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

EDIT:

About running silent files, you can do it too using NirCMD commandline application.

NirCMD exec hide "Path to Batch File\Batch File.bat"

Official site : http://www.nirsoft.net/utils/nircmd2.html

I think you need to run it through cmd.exe:

WshShell.Run("%COMSPEC% /c " & CurPath & "\Mybatchfile.bat", 0, True)

Check similar questions here and here for more information.

Have you tried using RunAs?

    cmds=WshShell.RUN("runas /noprofile /user:mymachine\administrator " _
    & sCurPath & "\Mybatchfile.bat", 0, True)

Since it works on Windows 7 but not on Server 2008 R2, It sounds like a permissions issue to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM