This question already has an answer here:
Prevent VBscript app from showing Console Window
(1 answer)
Closed 2 years ago.
I am working on shell script/ .vbs extension file, and I want to run a batch file hiddenly or in background from shell script. I am mentioning my code but its not executing a batch file. I have saved my file with a name test.vbs.
Set WshShell = CreateObject()
WshShell.Run chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Set WshShell = Nothing
Thanks in advance.
when you write CreateObject(), you have to mention in those circle brackets. Your code looks fine just add WScript.shell in circle brackets like this.
Set WshShell = CreateObject("WScript.shell")
You need CreateObject("WScript.Shell"), not CreateObject():
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Otherwise VBScript wouldn't know what you want to create! So it can't know you want the shell object that has the Run method...
Related
This question already has answers here:
Getting current directory in VBScript
(9 answers)
VBscript relative path
(3 answers)
Closed 8 months ago.
I have following vbscript:
set objShell = Createobject("wscript.shell")
objShell.Run """Scripts\Rec.bat""", 0
objShell.Run """Scripts\Prod.bat""", 0
Set objShell = Nothing
The above vbscript is stored in USB drive and i am trying to launch it from usb.
Now problem is when i normally run this vbscript it executes. But when i do run as administrator from context menu following error occurs "System cannot find the file specified"
I tried to debug the problem using another below script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
Wscript.Echo strDrive
When i run it normally it echo F: that is my usb drive letter.
But when i run as admin it echo C:
So the batch file location is being searced to C:\Scripts\Rec.bat instead of F:\Scripts\Rec.bat thats why this error is comming.
Can anyone help??
I want to run vbscript as admin and still retain the usb drive letter path because the code in my Rec.bat requires admin privilege
You don't want it to be relative to the current directory, you want it to be relative to the directory of the running vbscript:
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = "" & strFolder & "\Scripts\Rec.bat" & ""
objShell.Run strPath, 0
If you want to try it with a test script, you'd be advised to replace 0 on the last line with 1.
I am asking you guys how to run a .bat file from a .vbs without having to specify its containing drive C:. I want to try to make some sort of installation and I did a fake loading or installing screen that a .bat file opens up and does its mini animation from .vbs. The problem about this is when I send this .vbs with the .bat it always says that an error occurred; and this is what I found:
CreateObject("WScript.Shell").Run("""C:\Users\Name\Desktop\Thingy.bat""")
The problem with this script is that the person I send it to will also need the same name as I and in C:. How can I change this script, or is it impossible?
Next commented code snippet should do the job (most statements and commands are itemized for the sake of comprehensibility):
option explicit
On Error GoTo 0
' declare variables
Dim WshShell, sUserProfile, strCommand, fso
' assign all object references to appropriate variables
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' get an environment variable's expanded value
sUserProfile = WshShell.ExpandEnvironmentStrings("%UserProfile%")
' build command to run
strCommand = """" _
& fso.BuildPath( fso.BuildPath( sUserProfile, "Desktop"), "Thingy.bat") & """"
Wscript.Echo "(debug) command to run" & vbNewLine & strCommand
WshShell.Run strCommand
If the batch file is always on the desktop,you should use %userprofile% like this way :
CreateObject("WScript.Shell").Run(chr(34) & "%userprofile%\Desktop\Thingy.bat" & chr(34))
Or something like that when the batch file and the vbscript file are both in the same folder :
Set FSO = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(wscript.ScriptFullName)
wscript.echo scriptPath
MyBatchFile = chr(34) & scriptPath & "\Thingy.bat" & chr(34)
wscript.echo MyBatchFile
CreateObject("WScript.Shell").Run(MyBatchFile)
This question already has answers here:
Why oShell.Run not work
(2 answers)
Closed 6 years ago.
This code work fine
set oShell = CreateObject ("WScript.shell")
eAppData = oshell.ExpandEnvironmentStrings("%appdata%")
wscript.echo Appdata
oshell.run(Appdata & "\Test.bat"),0,False
This .vbs in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup
But when windows start show this message why ?
The Test.bat is in %appdata%\Test.bat
And when you get rid of this line : wscript.echo Appdata
This code will come like that if you don't want to display this message again
set oShell = CreateObject ("WScript.shell")
AppData = oshell.ExpandEnvironmentStrings("%appdata%")
oshell.run(Appdata & "\Test.bat"),0,False
You're using wscript.echo Appdata, which will cause this box to pop open containing the shown folder path.
I'm trying to run a .bat file using VBScript. I can get the VBScript to work when executed within the same folder as the .bat, however, I can't figure out how to make it successfully run when outside the folder.
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "C:\Users\js\Desktop\createIndex\createindex.bat"
Going out on a limb I would suspect that the batch script requires its own parent folder to be the working directory. You can set the working directory accordingly by changing your code to this:
Set shell = CreateObject("WScript.Shell")
shell.CurrentDirectory = "C:\Users\js\Desktop\createIndex"
shell.Run "createindex.bat"
If the above doesn't help you need to provide more information about what should happen and what actually does happen. Running the external command/script in visible mode and without automatically closing CMD usually helps with debugging:
shell.CurrentDirectory = "C:\Users\js\Desktop\createIndex"
shell.Run "cmd /k createindex.bat", 1, True
I've 4 simple scripts for what kind of calls.
call.vbs
call_nowait.vbs
call_nowindow.vbs
call_nowindow_nowait.vbs
All the 4 has these lines of code in the beginning:
ReDim args(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
If InStr(WScript.Arguments(i), " ") > 0 Then
args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
ElseIf WScript.Arguments(i) = "" Then
args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
Else
args(i) = WScript.Arguments(i)
End If
Next
Set objShell = WScript.CreateObject("WScript.Shell")
' MsgBox Join(args, " ")
Plus the last line is different in all 4 scripts:
call.vbs
objShell.Run Join(args, " "), 1, True
call_nowait.vbs
objShell.Run Join(args, " "), 1, False
call_nowindow.vbs
objShell.Run Join(args, " "), 0, True
call_nowindow_nowait.vbs
objShell.Run Join(args, " "), 0, False
For example, the entire command line for the call_nowindow.vbs script looks like this:
call_nowindow.vbs "C:\Users\js\Desktop\createIndex\createindex.bat"
By the way, you can run anything by these scripts, not only just batch files.
This one will create an instance of notepad.exe with out window:
call_nowindow.vbs notepad
This one will create cmd console with current date and time, and leave the window open:
call_nowait.vbs cmd /k echo.%DATE% %TIME%
Use this script to set the directory to the current working directory of your script. That way whenever the script changes directories it will look for it there instead of hard rooting it to a specific directory.
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
This will make any files that it is looking for in that directory available to the script.
Example : If the file were on the current users desktop.
C:\Users\[Username]\desktop\myfile.vbs
Will become:
scriptdir\myfile.vbs
and if you moved it to a subfolder "subfolder" it would remain
scriptdir\myfile.vbs
even if the script location is different. The "scriptdir" is a placeholder for the folder the active script is running from.
i need to make a batch file that have a hidden process and window for the users
i have already my batches file and want to make one of them hidden
vbs can't make that or any programming language ?
here you can find multiple ways to do this : https://stackoverflow.com/questions/28284876/what-are-the-different-ways-to-start-a-hidden-process-with-batch-file-and-what-a
Probably the best is with Win32_ProcessStartup as it returns also the pid of the started process.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\file.bat" & Chr(34), 0
Set WshShell = Nothing
with Vbs and call the bat file this is the way
Use the start command:
start /b my.bat
Refer to: https://technet.microsoft.com/en-us/library/cc770297(v=ws.11).aspx