Running vbscript from batch file - vbscript

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?

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

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

Just try this code:
start "" "C:\Users\DiPesh\Desktop\vbscript\welcome.vbs"
and save as .bat, it works for me

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

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)

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

Related

Windows command line: automatically type in a command from .cmd file

Okay, I have such a batchfile:
#title RUBY ;)
#set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
#call cmd /K cd /D E:\RubyProgramming
that I use to facilitate running scripts without the need to navigate to the folder each time. The thing is that I usually run the very same command for hundreds of times for a given program that I am working on at any given time. For instance:
ruby rubyprogram.rb inputfile.txt outputfile.xml miscargument
Is there a way to make such a batch file that types in a command when you run it? Not executes, just type in, so that I press Enter to execute it and use ↑ up arrow to use it again in the cmd? I haven't been able to find a command that would allow this anywhere, but it would be useful if there was one.
The simplest would be to just create a new batch-file that executes that specific command:
#echo off
title RUBY ;)
set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
cd /D E:\RubyProgramming
rubyprogram.rb inputfile.txt outputfile.xml miscargument
Alternatively, you could get the batch file to repeatedly ask for the command to run
#echo off
title RUBY ;)
set PATH=D:\Programming\Ruby22-x64\bin;%PATH%
cd /D E:\RubyProgramming
set RUBYCMD=rubyprogram.rb inputfile.txt outputfile.xml miscargument
:loop
echo.
REM line below ends with a space for neatness
set /p RUBYCMD=Enter ruby command (or 'Q' to exit) [%RUBYCMD%]:
if /i "%RUBYCMD%" == "q" goto :eof
%RUBYCMD%
goto :loop
No, batch files can't type or click anything. However, you can call scripts from a batch file which are written in other languages. For example, you cold write a VB or an AutoIt script, call it from your batch and make the new script "type" the command.
Take a look at this VB script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"
WshShell.SendKeys "hello world"
This will open notepad, focus the new window and type hello world. This way you can also start a new console or get the focus of an already started one and type your command. This code can be saved in a separate vb script file and called from your batch.

Stop CMD opening when bat file launched

I have this code
start /b error.vbs
When I run it, a CMD opens for half a second, then it runs. How would I remove or hide the CMD?
You don't need a batch file to launch a VBScript script. Just run the .vbs file directly with with wscript.exe (if you don't want the console). For example:
wscript error.vbs

Unable to run vbs file from windows registry

I have a registry key that calls a vbscript file as below.It will be triggered on selecting a Change Cursor option from right click on .cur file
"C:\Program Files\Cursor Manager\CustomCursor.vbs" "%1" 2
Below is the vbs file(CustomCursor.vbs)
Set objArgs = WScript.Arguments ' Create object.
CusorValue=objArgs(1)
Path=objArgs(0)
MsgBox CusorValue
MsgBox Path
But its not working.Its not showing any message
Its working when i am calling the script from command prompt like below.
"C:\Program Files\Cursor Manager\CustomCursor.vbs" "E:\new\CM v5\cursors new\more\Arrow.cur" 2
If the registry value is batch file its working
"C:\Program Files\Cursor Manager\CustomCursor.cmd" "%1" 2
Below is batch file(CustomCursor.cmd)
echo %1
echo %2
pause
But i can use batch file as it will show a command window,as it will show command window while executing script.
Please tell me a way to execute the vbs file from registry or atleast a way to run batch file in background.
Thank you
Change your registry to
"%windir%\system32\wscript.exe" "C:\Program Files\Cursor Manager\CustomCursor.vbs" "%1" 2
The problem is that invoking directly the script file makes windows search for the program to execute it and call the asociated executable with the script as argument, but in this process the rest of the arguments are discarded. Call yourself the script host with all the needed arguments.

copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when i double click

i am trying to execute a command xcopy path1 path2 /Y /C and it is getting executed successfully when i tried from command line and also when i copied it to a batch file and double clicking on that batch file.
But it is not getting executed when i cal this batch file from another fail.
Could anyone please help here?
Not working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
0 File(s) copied
Working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
C:\abcd\itsme.bat
1 File(s) copied
Extra Info:
Runme.bat:
call C:\folder1\copy.bat
call C:\folder1\clean.bat
copy.bat:
#echo off
xcopy "C:\folder1\runrun.bat" "Z:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "Y:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "X:\" /C /Y /Z /Q
Here, If I double click on Runme.bat, copy.bat is getting executed and copying all the files.
1 File(s) copied
1 File(s) copied
1 File(s) copied
But issue is, it is not copying anything when i try to run the same batch file from windows scheduler.
Output:
0 File(s) copied
0 File(s) copied
0 File(s) copied
looks like issue is only with copy command in the second batch file, which will return output. But all the commands in the other batch file clean.bat (which i am calling from the first batch file) are getting executed without any issues.
second batch file has simple echo commands, so that is why it is working fine.
Use xcopy with 'START' command.
For example, c:>> START xcopy "C:\folder1\itsme.bat" Y:\ /C /Y /Z
As Mofi pointed out, you can remove 'CMD'
The command cmd is for running a new instance of the command line interpreter and need to be used usually only for opening a command prompt window. Execute in a command prompt window cmd /? to get help about this command.
The command cmd does not need to be used usually if a command prompt window is opened already and a command is entered. A batch file is interpreted/processed by cmd and therefore it usually does not make sense to use cmd in a batch file.
So use only xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z in already opened command prompt window and in the batch file.
To process another batch file like batch file 2 from within a batch file like batch file 1 and continue processing of batch file 1 after processing of batch file 2 finished, use in batch file 1 the command call for calling batch file 2 like a subroutine.
Example for batch file 1:
#echo off
echo This is batch 1 calling now batch 2 and is waiting until it finished.
call "batch file 2.bat"
echo Batch 1 continues.
Example for batch file 2:
echo This is batch 2 running XCOPY.
xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z
echo XCOPY finished, batch 2 terminates.
Run batch file 1 and you get the output:
This is batch 1 calling now batch 2 and is waiting until it finished.
This is batch 2 running XCOPY.
XCOPY finished, batch 2 terminates.
Batch 1 continues.
Remove command call in batch file 1, execute it again and look what you get now.
This time without call in batch file 1 the processing of batch file 1 continues on batch file 2 without coming back to batch file 1 on reaching end of batch file 2 processing.
From your extra information it may be that x: y: z: are network drives and task scheduler doesn't have network privileges under the system account.
Also, change the name of copy.bat because copy is a system command and using system command names will bite you one day, if not today.

How to end a batch file with an open command window at a certain folder location

This seems like it should be ridiculously simple but I cannot find the answer online or in these forums.
I want to run a command from a batch file, leave the command window open and end at a particular file location.
I can't seem to get both to happen in the same window. This is for a script to run an automated task everytime and leave the window open to run a 2nd task that has a variable input.
start cmd /k c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\
If I run either by itself, they work (runs the exe, ends at /desktop prompt), but in this sequence only the first runs.
this works here:
#ECHO OFF
START /b "" "path\program.exe" "parameter"
CD %UserProfile%\Desktop
Do not use setlocal in your Batch or put endlocal in the line before the CD command.
This should work:
start cmd /k "c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\"
If you leave out the quote marks, the start command and the cd command are run separately.

Resources