32

I just need to write a simple batch file just to run a vbscript. Both the vbscript and the batch file are in the same folder and is in the SysWOW64 directory as the vbscript can only be execute in that directory. Currently my batch file is as follows:

@echo off
%WINDIR%\SysWOW64\cmd.exe
cscript necdaily.vbs

But the vbscript wasn't executed and just the command prompt is open. Can anyone tell me how can i execute the vbscript when i run this batch file?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
user918197
  • 1,129
  • 6
  • 17
  • 29

6 Answers6

54

You can use %~dp0 to get the path of the currently running batch file.

Edited to change directory to the VBS location before running

If you want the VBS to synchronously run in the same window, then

@echo off
pushd %~dp0
cscript necdaily.vbs

If you want the VBS to synchronously run in a new window, then

@echo off
pushd %~dp0
start /wait "" cmd /c cscript necdaily.vbs

If you want the VBS to asynchronously run in the same window, then

@echo off
pushd %~dp0
start /b "" cscript necdaily.vbs

If you want the VBS to asynchronously run in a new window, then

@echo off
pushd %~dp0
start "" cmd /c cscript necdaily.vbs
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 2
    @user918197 - ??? They work for me. Perhaps your VBS only works if the current directory matches the VBS location. Try the edits I made to the answer. – dbenham Aug 14 '12 at 11:37
  • does it have anything to do with 32-bit or 64-bit thing because my vbscript can't run in windows server 2008 R2 which is 64-bit and thats y i copy the whole folder containing the vbscripts into SysWOW64 directory and it was able to run when i type in the command, cscript necdaily.vbs but not in the batch file. – user918197 Aug 15 '12 at 00:56
  • Using `pushd` changes your working directory, which may be undesirable. Better do `cscript "%~dp0necdaily.vbs"`. Note there is no backslash between `%~dp0` and `necdaily.vbs`. – Gras Double Apr 17 '17 at 22:42
10

This is the command for the batch file and it can run the vbscript.

C:\Windows\SysWOW64\cmd.exe /c cscript C:\Windows\SysWOW64\...\necdaily.vbs
user918197
  • 1,129
  • 6
  • 17
  • 29
7

Just try this code:

start "" "C:\Users\DiPesh\Desktop\vbscript\welcome.vbs"

and save as .bat, it works for me

DIpesh
  • 71
  • 1
  • 1
5

Batch files are processed row by row and terminate whenever you call an executable directly.
- To make the batch file wait for the process to terminate and continue, put call in front of it.
- To make the batch file continue without waiting, put start "" in front of it.

I recommend using this single line script to accomplish your goal:

@call cscript "%~dp0necdaily.vbs"

(because this is a single line, you can use @ instead of @echo off)

If you believe your script can only be called from the SysWOW64 versions of cmd.exe, you might try:

@%WINDIR%\SysWOW64\cmd.exe /c call cscript "%~dp0necdaily.vbs"

If you need the window to remain, you can replace /c with /k

  • For the 1st command, maybe the [`call`](https://ss64.com/nt/call.html) isn't even needed. The script is also executed synchronously without it. Usually `call` is used to call a second *batch* script without mixing execution contexts. So, a tiny `@cscript "%~dp0necdaily.vbs"` does the job. – Gras Double Apr 17 '17 at 22:38
1

Well i am trying to open a .vbs within a batch file without having to click open but the answer to this question is ...

SET APPDATA=%CD%

start (your file here without the brackets with a .vbs if it is a vbd file)

desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

You should put your .bat file in the same folder as your .vbs file and call the following code inside the .bat file.

start cscript C:\filePath\File.vbs
TBR920
  • 77
  • 7