Hi Im tryijng to open a local htm file ussing vb script. I have the following code which will work for standard online webpages however my target htm is found localy, and in that case senario i cant get it to work
WORKING:
strURL = "http://www.somesite.com"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strURL)
NOT WORKING:
strURL = "file://J:\Project Phoenix\Tekenafspraak Tafelhandboek\COMPELATION WITHOUT IMAGES (MASTER)\Tekenafspraak Tafelhandboek.htm"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strURL)
You need to put quotes around your full path name if there are spaces in it. In this case, it means adding two extra double-quotes at the beginning and end of your string.
And you shouldn't need the "file://" in the path. The Shell object will just open the htm file in your default browser automatically:
strURL = """J:\Project Phoenix\Tekenafspraak Tafelhandboek\COMPELATION WITHOUT IMAGES (MASTER)\Tekenafspraak Tafelhandboek.htm"""
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strURL)
Quote your spaces. This is spaces 101. What will happen in your script will depend on the configuration of the computer it is run on.
Related
I created a logon script to automatically create a email signature for Outlook. This script creates the following file "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures\Default_signature.rtf"
I would like to add to a versionnumber to this file so I can check if an update of this file is necessary or not. If not, exit logon script. Else update signature with a higher versionnumber.
To get a versionnumber was pretty easy to find, but I couldn't find how to set/change a versionnumber with VBscript.
Using the DSOFile.OleDocumentProperties didn't work for me. I kept getting a ActiveX-error "Can't create object". It could be that I have to register
the dsofile.dll in the system but this code has to work for every user in our company and I don't want to install this on every device before I can use this logon script.
'Code for requesting versionnumber
Set objFSO = CreateObject("Scripting.FileSystemObject")
file = "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures\Default_signature.rtf"
Wscript.Echo "Version = " & objFSO.GetFileVersion(file)
I hope someone could tell me how to do this :)
As far as i know DSO is the only supported way by Microsoft to change properties of the files without rewriting them. There are some VBA scripts out there for Excel, but that does not apply in your case with RTFs.
However, might i suggest a different alternative? I have in my infrastructure a script that is placed to run at each logon and checks on a share a certain file's last modified date. If the file present on the machine is older, then it's replaced by that on the share.
Dim objFSO, strFileName
strFileName = "C:\Users\user\Desktop\Tests\fdsfsd.rtf"
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
WScript.Echo objFSO.GetFile( strFileName ).DateLastModified
Set objFSO = Nothing
At the top of most of my VBscripts (.vbs files) I have the following code:
Option Explicit
Const ForReading = 1
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
This code allows me to use another vbs file as a library. In this case somefile.vbs would be my library and have all my subs and functions defined that are called from the script the above code is called from (I call this the calling script).
This issue: Every once in a while, one of the scripts seems to delete the code in Z:\somepath\somefile.vbs (the library script read by the calling script).
I think this because if a wscript.exe is listed in my Task Manager Processes tab and I restore the Z:\somepath\somefile.vbs file from a backup location, almost immediately, when I open Z:\somepath\somefile.vbs again, there is no code in that file. But if I kill the wscript.exe process, the file is fine. I can't reproduce the behavior because it only occurs when our network has a hiccup of some kind (I think).
My first thought is that the create setting is wrong when I use this line:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
But according to this link, the default create value should be false:
https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
Note, coincidentally, I am also using objFile and objFSO variables in the file somefile.vbs for things that aren't related to what I am doing in the calling script. For example, the objFile in the somefile.vbs file has a completely different name and location and is created this way:
Set objFile = objFSO.OpenTextFile("z:\differentpath\differentname.vbs", ForAppending, True)
I am guessing this is the issue, but I don't understand it. Can someone shed some light on this? Is the create or append setting getting reset in the calling script? How does that work?
Not knowing what else to do I have change the variable names in the somefile.vbs file to oFSO, oFile and in the calling script they are still objFSO, objFile. I also changed the line of code in the calling script to include false for the create setting like this:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading,false)
Going out on a limb (since you posted only partial code) I'm going to assume that you don't explicitly close your library script after reading, so your main script keeps the file open until it terminates. Either add a line objFile.Close after the Execute statement, or (better yet) change
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
to
code = objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
Execute code
or just
Execute objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
so that the file is automatically closed after being read.
Learned from https://ss64.com/vb/execute.html
Execute takes a group of statements and executes them in local scope, ExecuteGlobal executes them in global scope.
However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global.
Does the issue remain if you call the Execute form a procedure?
Hello I've tried a lot of researching but cant find what I need and haven't been able to successfully piece this together myself.
Each of my users have a XML file within their profile that I would like to edit. The file contains a reference to their computer name and clientname, which are out of date each time they login to a new terminal. I need to replace these with the current computername and clientname. The bit I cannot figure out how to do is how to search the XML for the computername when I only know the first few characters, then replace it.
my XML will have any entry something like this
"InstalledPrinter name="\WHBCVDI0109\LabelPrinter650 (from IGEL-00E0C533943E)"
I need to search the file and replace the WHBCVDI0109 and the IGEL-00E0C533943E with the correct entries. My script successfully gets those entries I just dont know how to find and replace them in the file.
My script looks like this:
Const ForReading = 1
Const ForWriting = 2
Set oShell = CreateObject( "WScript.Shell" )
'Get Variables
user=oShell.ExpandEnvironmentStrings("%UserName%")
appdata=oShell.ExpandEnvironmentStrings("%appdata%")
strComputerName = oshell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Set XML location
strfile = appdata & "\Smart Label Printer\SlpUserConfig.xml"
'Open
Set objfso = CreateObject("Scripting.FileSystemObject")
Set filetxt = objfso.OpenTextFile(strfile, ForWriting)
strTemp = "HKEY_LOCAL_MACHINE\SOFTWARE\Citrix\ICA\Session\ClientName"
WScript.Echo "client name is : " & oShell.RegRead(strTemp)
An pointers would be much appreciated.
You shouldn't use the FileSystemObject and String/RegExp operations to edit an XML file. Using the canonical tool - msxml2.domdocument - is less error prone and scales much better.
See here for an example (edit text); or here for another one (edit attribute).
If you publish (the relevant parts of) your .XML file, I'm willing to add same demo code specific for your use case here.
In a couple of place in my code I check if the file exists (it does) then I try to Run the file as above, or get the DateLastModified, and get errors about file not found or invalid path. How can the script NOT see a file after confirming it exists?
I'm working up a .vbs script that tries to run an Access .mdb file. The WScript.Run command seems to choke on the filename, but putting a MsgBox() before that call to display the path allows Run to work properly. I don't want to display a popup.
Error is:
The directory name is invalid.
How is this possible and how can I get around it?
Here is code:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
SET ws = WScript.CreateObject("WScript.Shell")
path = Chr(34) & LocalPath & AccessFileName & Chr(34)
if (fso.FileExists(LocalPath & AccessFileName)) THEN
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run path 'This line errors
End If
Try to open your file with shell .InvokeVerb method:
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
If CreateObject("Scripting.FileSystemObject").FileExists(LocalPath & AccessFileName) Then
CreateObject("Shell.Application").Namespace(LocalPath).ParseName(AccessFileName).InvokeVerb
End If
UPD: Both ActiveX WScript.Shell and Shell.Application uses native windows shell to perform a file execution.The first one launches new process via WSH core located in wscript.exe, cscript.exe, wshom.ocx, jscript.dll, vbscript.dll, ets, .Run and .Exec methods of WsShell object provides wide control on the launched process, and second one located in Shell32.dll, uses .InvokeVerb method of IShellDispatch object, called without name, runs default verb equals to the windows explorer "open" command.In case of any issues connected to WSH, explorer might still works without any proplems. If it does, that is just a work-around, I can't say what's wrong definetely without close look.
Hello the following code worked for me.
Basically this code gets a folder object and loops through all files in a folder and checks if its the one that you named. This it runs the application.
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = Wscript.CreateObject("Wscript.Shell")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set myFolder = fso.GetFolder(LocalPath)
For each myFile in myFolder.Files
If myFile.Name = AccessFileName Then
'Wscript.Echo myFile.Name
ws.Run myFolder.Path & "\" & myFile.Name
End If
Next
You can give this a shot. You probably do not need the quotes around the path, but I included it as a comment if you want to give it a shot. You just put quotes twice if you need to include a quote character in a string:
Set fso = CreateObject("Scripting.FileSystemObject")
AccessFileName = "App.mdb"
LocalPath = "C:\Folder\"
Set ws = WScript.CreateObject("WScript.Shell")
' path = """" & LocalPath & AccessFileName & """" <-- probably unnecessary
path = LocalPath & AccessFileName
If (fso.FileExists(path)) Then
Set file = fso.GetFile(path)
'MsgBox(path) 'Uncommenting this line removes the error
ws.Run file.Path 'This line errors
End If
This does not make any sense. Having a MsgBox line is altering the behavior of the program!!!
I feel it is probably some weird invisible character somewhere which is getting activated when you comment the line.
Try retyping the If block without the MsgBox in between.
So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).