Calling exe in the VBScript - vbscript

I am trying to pass an exe path in the VBScript to call it automatically. Please suggest.
Path to pass :
C:\Program Files\TSVN\bin\Tor.exe"/command:repobrowser
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(""C:\Program Files\TSVN\bin\TProc.exe"/command:repobrowser"") 'Not working
Set objShell = Nothing

The quotes have not been escaped correctly causing in string to be incorrectly terminated, try this;
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("""C:\Program Files\TSVN\bin\TProc.exe""/command:repobrowser")
Set objShell = Nothing
The string is equivalent to
"C:\Program Files\TSVN\bin\TProc.exe"/command:repobrowser
Useful Links
Adding quotes to a string in VBScript

Related

Create a text file in %temp% and write content in it using vbs

Basically, I want to create a new file and write in it in a directory on the PC, pointed to by the %TEMP% variable. However, the revised code below does not work:
Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close
Error Message:
run time error
line no: 3
object required
Old Code
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)
If I use the file name "C:\myfile.txt" it works fine.
Error Message:
Path not found
In VBA, you can just use Environ("TEMP") to expand the Environment variable - if this does not work in VBScript, you may need to bind the WScript.Shell object and use the ExpandEnvironmentStrings property instead, like so:
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing
Following from comments below
Here is a "fully fixed" code:
'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String
'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need
'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need
you can use Environ("temp") to write to C:\Users\[username]\AppData\Local\Temp

CreateTextFile error

I am getting a 'Permission denied' error when trying to run this VBScript after dragging another file or folder icon onto the script's icon.
I have tried running it on two Windows 10 machines and if I run it with arguments if objects at the file creation line, otherwise the scripts completes normally.
Any ideas?
Option Explicit
Dim objFSO
Dim objShell
Dim fldTest
Dim fileTest
Dim strPath
Dim txsOutput
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txsOutput = objFSO.CreateTextFile("Test.txt")
For Each strPath In WScript.Arguments
fldTest = objFSO.GetFolder(strPath)
For Each fleTest In fldTest.Files
...
Next
Next
txsOutput.Close()
Set txsOutput = Nothing
Set objFSO = Nothing
WScript.Quit

VBS unzipping - object required: 'objshell.NameSpace(...)'

I know very little about bash or vbs. I am trying to make a script that will automatically unzip a zip called 'dungeon.zip', which contains a little game I programmed. I want to unzip it to a folder called dungeon in the same directory that the zip file was in. I used the code from this answer, and replaced the files with my files:
strZipFile = "dungeon.zip"
strUnzipped = "dungeon\"
Sub UnZip(ExtractTo,ZipFile)
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
fso.CreateFolder(ExtractTo)
End If
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(ZipFile).items
ObjShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing
End Sub
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("MyDocuments")
strZipPath = strDesktop & strZipFile
strUnzipPath = strDesktop & strUnzipped
UnZip strUnzipPath , strZipPath
As in his answer, I run the .vbs from a cmd file:
cscript UnzipZip.vbs
Here is the error:
C:\Users\Brett\Downloads\UnzipZip.vbs(12, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'
Any idea on how to fix this?
WshShell.SpecialFolders("MyDocuments") returns the path without a trailing backslash. You're then appending your filename to it.
You'll need to add a backslash.
strZipPath = strDesktop & "\" & strZipFile
strUnzipPath = strDesktop & "\" & strUnzipped
Edit to add a tip:
Use the BuildPath() function (it's part of FileSystemObject) to never have to worry about trailing backslashes again.
strZipPath = fso.BuildPath(strDesktop, strZipFile)
strUnzipPath = fso.BuildPath(strDesktop, strUnzipped)
Your ZipFile in
Set FilesInZip=objShell.NameSpace(ZipFile).items
is empty ('undefined'). Did you mean strZipFile?
You should use Option Explicit to avoid such blunders.
Set
strZipFile = "dungeon.zip\"
and
Set FilesInZip=objShell.NameSpace(strZipFile).items
in your code.
Just to elaborate the cause of the error. I also encountered the same scenario and it is because the zip file location is not valid or does not exists. Try to put the exact path of the zip file and it will work.

geting special folders with shell.application

I need in a script to return the path to the current user desktop. Now I know you can do it with WScript.
var WshShell = WScript.CreateObject("WScript.Shell");
strDesktop = WshShell.SpecialFolders("Desktop");
But for my script this will not work as I cant use WScript. but I can use the shell.application object as below.
dim objShell
dim ssfWINDOWS
dim objFolder
ssfWINDOWS = 0
set objShell = CreateObject("shell.application")
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
g_objIE.Document.All("logdir").Value = objFolderItem.path
end if
set objFolder = nothing
set objShell = nothing
what is the syntax so the rather than "BrowseForFolder" i can simple return the path of the current users desktop?
IE replace the line
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
with the equilivent of.
strDesktop = WshShell.SpecialFolders("Desktop");
Cheers
Aaron
You need to use Shell.Namespace(...).Self.Path:
Const ssfDESKTOPDIRECTORY = &h10
Set oShell = CreateObject("Shell.Application")
strDesktop = oShell.NameSpace(ssfDESKTOPDIRECTORY).Self.Path
WScript.Echo strDesktop
But for my script this will not work as I cant use WScript.
Do you mean you can't use WScript.CreateObject(...) because WScript is undefined? If so, you can simply use CreateObject("WScript.Shell").SpecialFolders("Desktop") instead. See What is the difference between CreateObject and Wscript.CreateObject?.
Try the namespace method:
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H10&)
Where &H10& is a special folder constant for the desktop. See technet for a list of all special folder constants.

Simple VBScript to open a application not working

here is my VBScript
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\Program Files\TrueCrypt\TrueCrypt.exe", 0 , false
It says it can't find it.
Taken from here
You can get around this by surrounding the path in quotes. But to do so, you need to escape them correctly(with "), so:
WshShell.Run """C:\Program Files\TrueCrypt\TrueCrypt.exe"""
Either put more quotes around the path, or use the old style string for "Program Files" - Progra~1.
The following example works on my machine:
<package>
<job id="truecrypt">
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files\TrueCrypt\TrueCrypt.exe""", 0 , false
</script>
</job>
</package>
You can try the following:
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""C:\Program Files\TrueCrypt\TrueCrypt.exe""")
Set objShell = Nothing

Resources