opening a file that's not in the same directory - windows

I want to run a program (that is not in the same directory as the script) with a MSG box, here's the code (it doesen't work because it needs the program to be on desktop like the script)
puls = MsgBox("Want to open steam?", vbYesNo + vbQuestion)
if puls = vbYes then
CreateObject("WScript.Shell").Run "C:\Program Files(x86)\Steam\Steam.exe"
CreateObject("WScript.Shell").Run "C:\Users\Dario Loi\AppData\Local\TeamSpeak 3 Client\ts3client_win64.exe"
else
MsgBox "Okay :(", vbInformation
end if
now, as you can see, i want to execute this at system startup to get my gaming programs running, but i can't put steam on the desktop because it will dump it's assets there, and it would be a mess, i can't put the script in steam's directory too, same thing for TS3, also, i tried to replace the name of the file with it's path, but it does not work either
EDIT:
Just for making things clear, i've got a VBS File on my desktop, along with it, on the desktop i've got 2 links (not the original EXEs), to steam and TS, (the programs i want to run), i would want to open these 2 programs without putting the script and the program in the same folder, so
by making the script refer to the link and
by making the script refer to the path,
if you got ideas, please tell me

And if you try like this ?
puls = MsgBox("Want to open steam?", vbYesNo + vbQuestion)
Set ws = CreateObject("WScript.Shell")
if puls = vbYes then
ws.Run DblQuote("C:\Program Files(x86)\Steam\Steam.exe")
ws.Run DblQuote("C:\Users\Dario Loi\AppData\Local\TeamSpeak 3 Client\ts3client_win64.exe")
else
MsgBox "Okay :(", vbInformation
end if
'*****************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*****************************************

Add a cd (change directory) line to the full path of your executables which will bring the cmd prompt to that directory, and do your run command.

Related

Failure to run VBS script with task scheduler

we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.

how to make my scripts portable vbs

this would be a noob question..but I want to make my scripts more portable.
lets say, I have coded all my .vbs(or any script in any code of nature) in a USB.
so I want to run that vbs on that current machine. the USB is assigned to be in F: drive
however when i unplugged that usb and sticked to another machine..it is going to be no longer F:..but it could be E: G: or whatever
I just wanted to know how do i overcome that without changing it directly on the scripts but the script is capable of reading which directory its pointing toward.
Im not sure how that property/functionality is called.
but would appreciate any hints/tips
There are two main ways you can make your script more portable.
The first, and probably the most appropriate for your use case, is to check what drive your executable is running on by using WScript.ScriptFullName and getting the first three characters to find the drive letter. You could alternatively chop the script name (WScript.ScriptName) off the end to find the current working directory. Assign this to a variable, and use it everywhere in your code where you specify a path.
Dim fullname : fullname = WScript.ScriptFullName
Dim drive : drive = Mid(fullname, 1, 3)
Dim path : path = Mid(fullname, 1, Len(fullname) - Len(WScript.ScriptName))
WScript.Echo "Drive = " & drive & vbcrlf & "Path = " & path
Another way is to make your script expect run-time arguments from the user, so you can specify exactly where you want to do things.
' Make a shortcut to your script and add a parameter,
' Or run from the command prompt with an additional parameter.
If WScript.Arguments.Length = 0 Then
WScript.Echo "No arguments supplied!"
WScript.Quit
End If
WScript.Echo WScript.Arguments(0)
I hope that helps!

Running DOS commands from Vbscript .Help needed with HTA

I have a vbscript that calls another from within itself. It works fine wen i run it from command line with cscript. For example the master vb script has a code such as
cmd /k cd "Path having the vbs" && cscript "slave_temp.vbs"
Now i have an HTA where the master vbs resides when i click on a button on the HTA this command is executed to call the slave.vbs
This is the command i use to call the slave.vbs
Set objShell = CreateObject("WScript.Shell")
runpath = "cmd.exe /K"& Chr(32) &"cd " & Chr(34)&final_path&Chr(34)&Chr(32)&Chr(38)&Chr(38)& Chr(32)& "cscript " &Chr(34)&Name1 &"_temp.vbs"&Chr(34)
objShell.Run runpath
Here final_path has the path name and Name1 has the file name of the slave.
I tried checking out the runpath value using Msgbox and it seemed all fine. But im dont understand why this doesnt work.
On a Final note the slave.vbs has SAP GUi automation code. and it throws this error when i use HTA to trigger
C:\path of slave\slave_temp.vbs(2, 4) (null): Invalid syntax
But the file executes properly when the slave_temp.vbs is executed separately. What can be the problem here.? Does this mean SAP objects dont have support wen run from HTA ?...
Help is much appreciated.
The following are the first few lines of the slave_temp.vbs having the SAP automation code
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Sorry for the Long question. But i think the issue is when calling SAPGUI object from HTA. Kindly help me tackle this.

Under what conditions does Wscript.Echo show up as popup instead of console

I was writing a vbs script which had an output to the console using the WScript.Echo command to let the user know what iteration the program was at. This worked fine until I was working on it again today when all of a suddent the script is echoing the message as a popup box. I stripped the code down to the bare bones and its still doing it and I am completely baffled.
Option Explicit
Call F
Sub F()
Dim TimeInterval
Dim CycleCount
Dim CycleCounter
TimeInterval=FormatNumber(WScript.Arguments(0),0)
CycleCount=WScript.Arguments(1)
CycleCounter = 0
For CycleCounter = 1 To CycleCount
WScript.Sleep(TimeInterval)
WScript.Echo "Iteration " & CycleCounter & " / " & CycleCount
Next
End Sub
I don't see it being the code that is causing it but rather the environment it is being run in but I'm not sure how I'd go about checking where to alter the settings or how the settings were even altered in the first place.
It will display a popup if you're using Wscript.exe to run your script. It will display in the console if you're using CScript.exe to run your script.
See this (among many other resources on the web): Should we use CScript.exe or WScript.exe to run .vbs files?

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.

Resources