VBscript ends without user input - vbscript

I am having a problem with VB scripts on a Windows 7 computer. When I try and run any *.vbs file, it runs the script but does not pause for user input. The code below runs but it does not pause for the echo statement. On other computers it will pause for the user to press the Ok button. What do I need to change on the computer which does not allow it to pause?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
strPath = "c:\temp"
Set objFolder = objFSO.GetFolder(strPath)
Wscript.Echo objFolder.Size
or
Input = InputBox("Enter Project Number: ")
Wscript.Echo Input

Map your VBScript to wscript.exe instead of cscript.exe.
Right click -> Open With... -> Ensure "Always use the selected program to open this kind of file" check box is checked -> Click the "Browse..." button -> "C:\Windows\System32\wscript.exe" -> Open -> OK

As the mapping to CScript is a good suggestion and should work, as an alternative you can use the MsgBox statement in your script. This shows a message box independently if you are running under CScript or WScript:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
strPath = "c:\temp"
Set objFolder = objFSO.GetFolder(strPath)
MsgBox objFolder.Size

Related

Declare VBScript inside of VBScript (Running multiple .VBS at a time)

I need to run multiple .vbs at a time. This is the only way I was able to find online:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "1.vbs"
objShell.Run "2.vbs"
objShell.Run "3.vbs"
Set objShell = Nothing
To do that I'd have create 1.vbs, 2.vbs and 3.vbs separately. Is there a way to input them all as part of one .vbs file? Something like
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim vbs1 as vbscript
Dim vbs2 as vbscript
Dim vbs3 as vbscript
Set vbs1 =
"Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\test1.xlsm'!Module1.refresh"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing"
Set vbs2 = ' whatever code
Set vbs3 = ' whatever code
objShell.Run vbs1
objShell.Run vbs2
objShell.Run vbs3
Set objShell = Nothing
The purpose of this:
I have ~50 excel reports with connections to SQL that need to be updated every day.
To do that I've created a macro and added it to each of them. The macro refreshes connections/queries > refreshes pivot tables > removes the connections/queries > saves as a macro-free workbook in a specified location.
I wrote .vbs scripts for each report. It just runs that macro in every Excel workbook.
Instead of running every .vbs separately, I've created one main .vbs that references all prior created .vbs to run them at the same time.
My question is if there's a way to do that without creating 50 separate .vbs files.

Windows 7 won't refresh folder automaticly, made a vbs script to refresh by using Sendkey

Because win7 won't refresh my folder where i store log files, so i found a script online but it doesn't seem to work.
Sub RefreshSavedFiles()
Dim oShellObject
Set oShellObject = CreateObject("Wscript.Shell")
strFolder = "C:\Users\User\Documents\PDF files saved"
oShellObject.AppActivate strFolder
oShellObject.SendKeys "{F5}"
End Sub
i have a backup program that calls this script before it backups the file to my server.
This enumerates open folders and refreshes the two specified.
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
If window.locationname = "C:\" or window.locationname = "website2.com" then
window.refresh2 3
End If
Next

Delete file within APPDATA folder

I have many PCs that currently have a Personal macro workbook installed. More specifically, they all have a shortcut to a Personal macro workbook on a network drive.
To install that, I went to each PC and ran this VBScript:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
oFSO.CopyFile "H:\Folder\Folder\Folder\PERSONAL 1.xlam - Shortcut.lnk", strAppData, True
Set objShell = Nothing
Set oFSO = Nothing
Now though, I want to remove that shortcut to PERSONAL 1.xlam from the XLSTART folder and copy over a shortcut to a different macro workbook.
This might be really easy, but I'm new to VBS and I haven't found a way to delete a file without having the the exact path. And since the path is going to be unique to each PC, I can't do that here.
You just need to modify one line from the above script. Try the following:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
'here is the modified line
oFSO.DeleteFile strAppData & "PERSONAL 1.xlam - Shortcut.lnk", True
Set objShell = Nothing
Set oFSO = Nothing

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

VB Script to open multiple programs at once

Right im looking for a script that I can click on after I have logged in to open various programs just to save me a bit of time. I have managed to get a script to open one but as a bit of a newbie can someone provide advice.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe "" ""-
express:dvla.servicecenter.fs.fujitsu.com.12680"""
Set objShell = Nothing
You might be overthinking it a bit to use VBScript or Powershell for this job. A batch file will work.
#echo off
start "c:\Program Files\Folder 1\program.exe"
start "c:\Program Files\Folder 2\program.exe" -switch -argument
exit
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("Path to program")
wscript.sleep (100)
objShell.Run("Path to program")
wscript.sleep (100)
wscript.quit
I do not have scguiw32.exe, so I created simple script which opens file in notepad and in word.
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run "C:\Windows\notepad.exe c:\dump.txt"
objShell.Run """C:\Program Files (x86)\Microsoft Office\Office14\winword.exe"" c:\dump.txt"
Set objShell = Nothing
BTW Instead of vbscript you can use now powershell, and powershell script is much more easy to understand. For example above one will be: Create run.ps1 with content
& 'C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE' c:\dump.txt
notepad.exe C:\dump.txt
Click it right-click and choose Run with Powershell
Here is how to use vbscript to create an array of programs you want to run and then execute each one.
'---Declare Variables
Dim objShell, strprogram1, strProgram2, colprograms, item
'---Create Scripting Shell Object
Set objShell = CreateObject("WScript.Shell")
'---Create Program Variables
strProgram1 = """C:\Program Files (x86)\servicecenter\Run\scguiw32.exe"" ""-express:dvla.servicecenter.fs.fujitsu.com.12680"""
strProgram2 = "C:\Windows\notepad.exe C:\Dump.txt"
'---Add Variables to an Array
colPrograms = Array(strProgram1,strProgram2)
'---Run each program in the array once
For Each item In colprograms
objShell.Run item
Next
WScript.Echo "Done."

Resources