Running a Batch File in the Same Directory from the VBS - batch-file

I have a VBScript that is supposed to run a .bat or .vbs file, but it doesn't work!
It comes up with an error saying that the file could not be found, whether i put in a file path or not (it shouldn't matter anyway I think because it's in the same directory).
So my question is, how do I start a .bat file (or even better, a .vbs file) from within a VBScript?
The relevant code is bellow:
'*******This is the start of my open command that doesn't work*******
Do
If Hour(Now) >= 9 And Hour(Now) <= 18 And Minute(Now) = 34 And Second(Now) = 59 Then
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "F:\\EAS\Volume Up.vbs"
Set shell = Nothing
MsgBox "My De-bug Message Box which doesn' even get to open"
WScript.Sleep 2000
Set WshShell = CreateObject("WScript.Shell")
music = "C:\...\MYFILE.wav"
WshShell.Run "wmplayer """ & music & """", 0, True
WScript.Quit 1
Else
'*******This is the end*******
So what am I doing wrong? Is it the wrong way to open it? What should I put instead?

It would be good to see the contents of the .BAT File.
Your code seems fine as I am able to run the below script on my machine:
dim shell
set shell=createobject("wscript.shell")
shell.run "tester.bat"
You may not see what the .BAT File is doing as it happens so quickly, as a tester add the following command to the end of your .BAT Script:
pause
Then you will see the command prompt open. As per my VB code above, the .BAT file contents are below:
#echo OFF
#echo %time%
pause
This will show you the current time and then pause, leaving the command prompt open. Give this a go as a tester as it works fine for me.

putting triple quotes (as suggested by ToThePoint) around the path solved my vbs file error, where it was failed to find the file on specified path as file path was having spaces, like
D:\Main\My text Files\abc.txt.
thanks :)

The only thing i can think of is that it must be a typo in the file name.
Can you ensure the file name is spelled correct?
Or else please post the exact error you get.

Related

My BAT file does not execute my executable file

I have a code witch I want to starts when windows starts so I added bat file in
C:\Users\Admin\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
directory and my bat file is like:
start "random title" "C:\test\dist\test.exe"
this code works when I use it in cmd but in bat it only shows terminal for like 0.01 second then it close itself without run my code I have added cmd.exe at the end of my code just to see the error log but did not worked
then tried pause cmd-k and other solutions
any idea would be very usefull
also I am searching this for days so I guess I have checked every source I could find so this question seems like dublicate but other questions did not solved my problem
At the end of the day I ended up with creating a shortcut as Compo said here is my code:
path = os.path.join(bat_path, 'windowsStartup.lnk')
target = file_path
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WindowStyle = 1
shortcut.save()

Opening project in VSCode using batch file

Disclaimer: I read this and this before, but it doesn't work as I want.
Description: I decided to create set of batch files for convenient way to run different projects in VSCode from desktop in one click(double-click). I want close cmd terminal after running a batch file, but terminal remains. I tried:
start code "C:\Users\MyUserName\path\to\my\project\directory"
and
cmd /c start code "C:\Users\MyUserName\path\to\my\project\directory"
It quickly runs command, runs code and opens my project, then, it seems to me, closes terminal and runs a new one in desktop directory.
I found solution with help of DavidPostill. This works fine for me:
start "" cmd /b /c code "C:\Users\MyUserName\path\to\my\project\directory" && exit 0
UPDATE:
There is a more simple way to run VSCode using command line interface:
cd path/to/my/project
code .
If anyone else comes across this in 2022, I found a solution that works great for me.
code "" "C:\path\to\folder\with\project" | exit
Also, below is my batch I made for a quick workspace that:
asks for a folder name, this will also be used as the project name
makes the vscode project
makes 2 text files, one for things I had to look up, and another for answers to my question
makes a png file called work.png that opens with paint for diagrams I might need for thinking through things
Lastly (the part I love the most) it CLOSES the command window once everything is opened!
Hope this helps someone like me who doesn't know everything about batch files!
#echo off &setlocal
set /p "folder=Enter the folder name to be created: "
md "%folder%" ||(pause &goto :eof)
cd %folder%
echo. > Things_I_had_to_look_up.txt
echo. > Answers.txt
dotnet new console
xcopy /s "C:\xPaintFileTemplate" "C:\Users\TBD\Documents\Program Work\%folder%"
start mspaint.exe Work.png
code "" "C:\Users\TBD\Documents\Program Work\%folder%" | exit
Rem not needed but to be safe
exit
The C:\xPaintFileTemplate is a folder in my C drive that contains only a png file named Work.png. The png file is blank, and sized to what I want mspaint to open with. If there are more files in that folder, it will copy all of them, so be careful with xcopy!
The Rem is a comment, saying the exit command doesn't seem to be required but I added it in anyways as I believe it is good practice.
Try using: start cmd /C code . :0 It should be able to close the cmd terminal. At least that worked for me on my Windows 10.
Another version:
start cmd /C code "C:\Users\MyUserName\path\to\my\project\directory" :0
Based on Blake Daugherty's answer, I found the first pair of double quotes seems unnecessary:
code "D:\proj\directory-1" | exit
code "D:\proj\directory-2" | exit
exit
None of the above worked for me; at worst one of the left-over CMD's needed its close button clicking three times before it would go away.
I tried the URL method and this worked just as I wanted: VSCode opened, and the cmd window went away; my batch file ("VSCode on project.bat") contains just one line:
start vscode://file/C:/path/to/project
or:
start "" "vscode://file/C:/path/to/project"
One line code:
code C:\Users\MyUserName\path\to\my\project\directory pause

How do i prevent a VBScript from running standalone?

I'm doing a mash between VbScript and CMD, i can call the VBScript easily with
cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt"
But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than it being called through a batch file.
My first attempt was a mess of setting a variable into a text file before i ran cscript.exe and use error handling in VBScript to tell if that variable could be collected, but it added too much time to the script.
Does VBScript have a way to tell whether it was started by CMD, or simply by double clicking, and able to act accordingly?
Here is a simple function, detecting the parent process caption. You can check if the process was started by CMD shell (the caption is cmd.exe) or by double-click (explorer.exe):
If LCase(GetParentProcessCaption()) <> "cmd.exe" Then WScript.Quit
' the rest part of your code here
Function GetParentProcessCaption()
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & CreateObject("WScript.Shell").Exec("rundll32 kernel32,Sleep").ProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
GetParentProcessCaption = .Caption
End With
End With
.Terminate
End With
End Function
In the context of your question another method allowing to pass parameters from CMD shell process to WSH script child process may be useful. It uses environment variable and WScript.Shell object. Consider the below example.
There is code for task.cmd file:
set myvar=myvalue
wscript "%~dp0task.vbs"
And for task.vbs file:
WScript.Echo CreateObject("WScript.Shell").Environment("process").Item("myvar")
I have got the output as follows:
Note, process environment variables are accessible for child processes only.
One way is for your VBS file to check for the presence of parameters and if they do not exist then stop the execution.
In your VBS script:
If WScript.Arguments.Count = 0 then
' No parameters provided. Can stop here.
End If
When you call your VBS file, just passing any parameter will satisfy the condition:
REM This will work.
cscript.exe //NoLogo "%~dp0TASK.vbs" "hello world"
REM So will this.
cscript.exe //NoLogo "%~dp0TASK.vbs" 1 2 3 4
REM This will not.
cscript.exe //NoLogo "%~dp0TASK.vbs"
This will not stop people from running it manually (with a parameter) or creating a shortcut which has a parameter. It would only really stop running the VBS directly (as a parameter will not be passed).
When you double click on a .vbs file, the action is determined by the following registry key:
Computer\HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command
If you were to change the key, you will be changing the double click action, but you will not be affecting your ability to launch the command explicitly via invoking cscript.exe directly.
If the bat file will keep the cmd.exe open while the vbs file runs, you can try to detect the cmd process inside the vbs file to continue execution.
Put this at the start of your vbs file:
Set shell = CreateObject("WScript.Shell")
list_str = shell.Exec("tasklist").stdOut.ReadAll 'get a list of processes by calling the windows program 'tasklist.exe'
If InStr(list_str, "cmd.exe") = 0 Then WScript.Quit 'quit if process is not found

Open ie from command line invisibly

I'm trying to open internet explorer invisibly frim command line. The page I will open log user data for our software. Thats why I want to make it invisible, there is nothing necessary for people on the page.
I tried this,
start iexplore http://www.example.com
it works. However, its visible. (I know there are some working scripts in vbs , shell. But I need to make it from command line) How to make it invisible ?
You can't with batch.
This is a vbs script. Just execute it.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """c:\program files\internet explorer\iexplore"" www.google.com", 0, false

Redirecting shell application output to a file with HTA application

Can someone tell me why redirecting to a file does not work in my HTA program? It contains the following:
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "md5sums.exe", "tarball.tar > .\md5sum.log", , , NORMAL_WINDOW
When I run my HTA program with the two above lines without the redirect it works fine. But the second I add the "> .\md5sum.log", md5sums.exe spits out the following error:
Unable to read file/directory .\md5sum.log
, meaning it's ignoring the redirect symbol and trying to take the checksum of the non-existent md5sum.log file.
md5sums.exe is simply an external checksum program. When I run the following from a command line, it works fine:
md5sums.exe tarball.tar > .\md5sum.log
, piping the checksum for tarball.tar to the md5sum.log file as expected.
I've searched high and low throughout the Interwebs, without finding a solution. I'd greatly appreciate any help anyone can provide.
Finally figured it out! In case anyone runs into this:
shellCmd = "cmd /c md5sums.exe ""tarball.tar"" > ""md5sum.log"""
Set shell = CreateObject("WScript.Shell")
shell.Run shellCmd

Resources