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.
Related
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.
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
I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John
Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""
Hi I'm trying to run this script but it's giving me 'Exepected end of statement.
wscript.exe "C:\test.vbs" "your_file.bat"
I'm putting the above in run.vbs, and I'm trying execute run.vbs from withing windows by double clicking the file. I get 'Expected end of statement' error
in the invis.vbs there is :
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Earlier I got this solution from : Running Batch File in background when windows boots up
Please advise.
The wscript.exe "c:\test.vbs" "your_file.bat" you can't put in run.vbs. It's batch script. Put that in a run.bat....
or
Modify run.vbs to look like:
Set objShell = CreateObject("WScript.Shell")
objShell.run("wscript C:\test.vbs your_file.bat")
This
wscript.exe "C:\test.vbs" "your_file.bat"
is meant for textual input in the command prompt/console/dos box window. If you put it into a .VBS file, you (deserve and) get a syntax error. This:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
looks like valid VBScript code (whether in invis.vbs or test.vbs).
Trying to run a .VBS by double click is a bad idea if you plan to pass parameters to the script.
So you should start afresh and think about/describe clearly what you want to achieve.
Is it possible to call a .vbs script from a VBA code whenever needed? If possible then can you give me a sample code of how to do so?
To run a file:
Shell "wscript c:\null\a.vbs", vbNormalFocus
replacing wscript with cscript if the VBS wants to use the console.
Or you can add a reference to the Microsoft Script Control and interact with the VBScript runtime directly to execute VBS code, procedures etc;
Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"
scr.AddCode "sub T: msgbox ""All Hail Cthulhu"": end sub"
scr.Run "T"
I only want to add to Alex' answer, that in some environments the object must be created in the following way:
set scr = CreateObject("MSScriptControl.ScriptControl")
If Alex adds this to his answer, I will delete this one.
Try somthing like this
ChDir ThisWorkbook.Path
Shell "wscript " & ThisWorkbook.Path & "\your.vbs", vbNormalFocus
It helped me.
You may want to use """ if your path name contains a - For me the following solved this issue:
Shell "cscript """ & ActiveWorkbook.Path & """\your.vbs", vbNormalFocus