VB script want to run .bat file from VB - vbscript

I am try to run the go.bat from VB but when I run the script I get: :cant find specific file
but from the cmd window the file go.bat exist. what the problem?
Dim MyShell
Dim shell_cmd
shell_cmd = "C:\Program Files\dir1\dir2\wizard\go.bat"
set MyShell = CreateObject("WScript.Shell")
MyShell.Run shell_cmd, 1, 1
from cmd window
C:\Program Files\dir1\dir2\wizard>go.bat

Your batch file's full path contains spaces, so you need to enclose it in double quotes, like this:
shell_cmd = """C:\Program Files\dir1\dir2\wizard\go.bat"""
or
shell_cmd = Chr(34) & "C:\Program Files\dir1\dir2\wizard\go.bat" & Chr(34)

not sure if you knew but in vb you can use the Shell function:
http://msdn.microsoft.com/en-us/library/xe736fyk(VS.71).aspx
(seems easier than what you're using)
I don't know why you get this message, but the two paths you mention are in fact different:
C:\Program Files\dir1\dir2\wizard\go.bat
C:\Program Files\dir1\dir2\wizard>go.bat
^

Related

Find/Replace - Not working when called from batch file. OK if run independently

I have a question based on a previus question asked here.
I need to change registry key entries on multiple computers where REG GUIDs of said registry keys are different on every machine. I have found that I can pull the regkey into a .reg or .txt file, find the “Device_State” values, and replace them with what I want using a .vbs script similar to the one in the link I referenced above.
I cannot use any 3rd party solutions for this. It must be organic to Windows, by nature (Windows 7 and windows 10 is currently my env).
Problem
My Test.vbs script appears to work when I just have the .reg file in a directory, and run (double-click) the .vbs script (from same directory).
The script finds and replaces the strings I want and writes it all back to the .reg file (in this case, "replace.reg"). However, when I call it from my batch file, the .vbs script returns an error:
Line: 15 Char: 1 Error: Invalid procedure call or argument
Code: 800A0005 Source: Microsoft VBScrpt runtime error
line 15, 1 in my code is: objFile.Write strNewText.
When I check the .reg to see what has changed, the file is now blank. It seems like the script is reading the content of the file into the strText variable, finding my values, replacing values, then erroring out and not writing back to the .reg file.
Batch file
My batch file does the following:
creates a directory for backup and to work out of
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg
exports the registry key to a .reg file (and a separate .txt for reference) for actual manipulation of file content:
reg export “hkey_name” c:\windows\patches\replace.reg
reg export "hkey_name" c:\windows\patches\replace.txt
calls the .vbs script
#call cscript “%~dp0Test.vbs”
I have also tried
runas /user:localadmin /savecred "wscript.exe c:\windows\Patches\Test.vbs"
imports the new .reg into the registry (this is the plan; have not gotten this far yet).
reg import c:\windows\patches\replace.reg
VBScript
The Test.vbs script is as follows:
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpentTextFile(“C:\Windows\Patches\backup.reg”, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, ”HKEY”, ”BLAH-BLAH”) '<—– this is just for testing function!
strNewText = Replace(strText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
objFile.Write strNewText
objFile.Close
MsgBox "Done"
So, again, to reiterate, the VBScript does exactly what I want when I run it independently. But when I call it from a batch file, it "wipes out" (or fails to write to) the file I need changed.
I am running this as an administrator of the local system, with full rights to the files in question.
I am running this from an elevated CMD prompt/window.
Question
Does anyone know why this would work independently of the batch file, yet not work when called by the batch file? I'm thinking it has to have something to do with permissions or authority to open/close the file for reading/writing while "inside" a batch routine. I just don't know enough to figure it out.
***************************** UPDATE ************************************
I have narrowed this down even further and here is what I'm finding:
-
1) I run my batch file that creates a backup of the registry keys I need to edit. The batch file creates a .reg file and a .txt file of the same registry keys (this is just for my own security to have both). I'm also granting full rights to everyone, users, administrators on this folder/files
2) I then run the find/replace text vbScript. It errors out on the line "objFile.Write strNewText". When it errors out it appears to wipe out the contents of the .reg file.
3) I found that if I take everything in the .txt file (same exact text) and paste into the .reg file (AFTER the vbscript errors out) and THEN run my script, it works.
I have tried manually deleting the .reg contents and replacing it with the .txt content and trying to run the script, and it errors out the same way and again appears to wipe out the contents of the .reg file. It's ONLY AFTER THE VBSCRIPT errors out on writing to the file, appears to wipe out the contents, that I can paste the contents of the .txt file into the .reg file and run the VBScript ... and it works. I'm so confused by this. I can't imagine what's going on. Please, I'm hoping someone else can help me. Thanks.
There is likely an error being thrown that you can't see. In your batch file, at the end of each line, add > output.txt, like so:
md c:\windows\patches
reg export "hkey_name" C:\windows\patches\backup\backup.reg > outputExport1.txt
reg export "hkey_name" c:\windows\patches\replace.reg > outputExport2.txt
reg export "hkey_name" c:\windows\patches\replace.txt > outputExport3.txt
cscript "%~dp0Test.vbs" > outputVBS.txt
This will dump any error information you would have seen in the command prompt to the output file. Alternatively, you can manually run each line of your batch file in a command prompt and see what it says there.
Inside your script, you can add additional debugging outputs (and they will also either be dumped into your output file or display in the command prompt if you run it manually):
Option Explicit
Dim objFSO, objFile, strText, strNewText
Const ForReading = 1
Const ForWriting = 2
WScript.Echo "Reading the file"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpentTextFile("C:\Windows\Patches\backup.reg", ForReading)
strText = objFile.ReadAll
WScript.Echo "File Contents: " & strText
objFile.Close
strNewText = Replace(strText, "HKEY", "BLAH-BLAH") '<—– this is just for testing function!
WScript.Echo "Did first replace: " & strNewText
strNewText = Replace(strNewText, "LOCAL", "EAT_SOME_FOOD") '<—-just for testing function!
WScript.Echo "Did second replace: " & strNewText
Set objFile = objFSO.OpenTextFile("C:\Windows\Patches\backup.reg", ForWriting)
WScript.Echo "Opened the File for Writing"
objFile.Write strNewText
WScript.Echo "Wrote the File"
objFile.Close
WScript.Echo "Closed the File"
WScript.Echo "Done"
Note that in the code above I corrected a bug you have in the second Replace - in the original code you are reusing the strText variable, which means the changes done by the first Replace are overwritten.

run 2 dos commands using vbScript as a single command

I am trying to change the directory and then run another command all
in 1 line. But for some strange reason I can't even get the first
command to work. I need help!!
What below code is does, first it launch cmd promt, change directory to directory where my jar file is situated ,that jar file take 3 arguments, all arguments are folder path so it enter java -jar myJar.jar C:\folder1 C:\folder2 C:\folder3
dim objShell
dim jarFileFolder, outFile, projFile,folderC ,cmd1,cmd2
'setting variuos paths
jarFileFolder ="C:\temp\"
'2 commands
cmd1= "cd jarFileFolder"
jarCommand = "java -jar myJar.jar C:\folder1 C:\folder2 C:\folder3"
set objshell = createobject("Wscript.shell")
objshell.Run "%compsec% /k cmd1 & jarCommand"
Below is the link to screenshot of the command i want to execute from vbscript
cmd to be executed from the cmd prompt
I'm writing this as an answer as I don't have enough rep to comment. You have a typo which might be causing the problem of the command not executing - compsec should be comspec.
The problem with your first command is that you are not using the jarFileFolder variable, but instead trying to change to a folder named "jarFileFolder". You should try:
'setting variuos paths
jarFileFolder ="C:\temp\"
'2 commands
cmd1= "cd " & jarFileFolder
And then when you run objShell:
set objshell = WScript.CreateObject ("WScript.Shell")
objshell.Run "%compsec% /k " & cmd1 & " " & jarCommand

Double Click and Open an undefined file from within VBscript

I want to open .mp3 files with mpg123.exe silently when a .mp3 file is double clicked from within Windows Explorer. For this I wrote a VBScript as bellow and changed the default program for playing .mp3 files by assigning my VBScript to it via Open with → Choose default program. My script is working well from within command line (cmd.exe) but when a .mp3 file is double clicked I get an error message that double clicked .mp3 file is not an executable file in Windows. Here is my VBScript, please let me know where I am doing wrong.
if Wscript.arguments.count = 0 then
WScript.quit
else
strSoundFile = WScript.Arguments.Item(0)
end if
Set objShell = CreateObject("Wscript.Shell")
strCommand = "mpg123.exe -q " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
Why don't you just associate mp3 files with mpg123.exe and set up the associated parameters (eg: -q "%1") instead?
Since I couldn't find a notable existing example, I've whipped up an example for you. (tested to work on Windows 7 with mpg123.exe). The response was too image heavy to post here. I hope it helps you.

Get full path of VBScript script file when WScript not available

I'd like to get the full path to the script file I am executing. A quick google search shows me that WScript.ScriptFullName will do the trick. The problem is that I am executing this script outside of the Windows Script Host (I'm using ScriptUnit) and so if I try using that code I get the error:
Variable is undefined: 'wscript'
How do I get the full path to the current script file if I can't use WScript?
Can't you shell out, start a small script with cscript and pass the value to the parentscript throug an environmentvariable ? Can't test this since i don't have or use ScriptUnit.
Obviously the following would not work sincve you don't have Wscript with you but has ScriptUnit a similar feature to read environmentvars ?
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CScript.exe " & """" & ScriptName & """" 'this would set your environmentvar scriptpath
Set objProcessEnv = WshShell.Environment("Process")
path = objProcessEnv("scriptpath")
Or start the script from a batch or other script, record the path in a text-file and read the contents of that file in your mainscript.

Wscript.Shell Run doesn't work consistently

I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?
You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus
You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

Resources