Creating a Message Box that runs a Specified Program (vbs) - batch-file

Take the following script that I have:
x=msgbox ("Do you want to recycle the Premiere Pro Media Cache?" ,4, "Recycle Premiere Pro Media Cache")
If box =6 Then
CreateObject("wscript.shell").run "C:\Expedited\Scripts\PrMCRecycler1"
End If
My goal is to get this VBS file (which brings up a message box) to run the batch file (the same way it would run when double-clicking it) when the yes button is pressed. I'm not sure what I'm doing wrong above. When I click No, nothing needs to happen, so I didn't specify anything for it.
Basically, since it brings up a yes/no message box, I just need to make the yes button execute the specified batch file. I could really use some assistance in figuring out what's wrong. When I try the code listed above, nothing happens upon choosing yes (besides the dialogue box going away).

Try this example and change the path of your batch file.
Option Explicit
Dim ws,Question,PathProgram
Set ws = CreateObject("wscript.shell")
'change the path of your batch file
PathProgram = "C:\Program Files\Internet Explorer\iexplore.exe"
Question = Msgbox("Do you want to recycle the Premiere Pro Media Cache?",VbYesNO + VbQuestion, "Recycle Premiere Pro Media Cache")
If Question = VbYes Then
ws.run DblQuote(PathProgram)
End If
'***************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************

Related

Copy New Access client to users

I use a very simple version control whereby I use the "tag" property on the Switchboard to record the version of the database.
On a linked "mastertable" I have a master record that shows the current version. If this is out of sync, then there is code in the switchboard to initiate a simple file copy of the new client version to the user's desktop.
I can't use any EXE type auto-installers to do this, so had to come up with an all Access solution, but having some issues with shell commands and timing that is causing the "auto" part of the installer to be inconsistent.
Below are my codes for the Client and my standalone "Installer" database (which only has one form that opens on startup and initiates the copy code). I use lookup tables for all file locations, but will use strings in my example.
Client.mdb:
strInstaller = "c:\Installer\Installer.mdb"
set obj = CreateObject("access.application") 'previously tried SHELL command
with obj
.visible = true
.userControl = true
.openCurrentDatabase (strInstaller)
end with
application.quit
Simple enough. So the above code just opens my Installer mdb which opens a form and executes the following on open.
Installer.mdb:
strFileName = "ClientDB"
strMaster = "D:\" & strFileName
strClient= dlookup("DBPath", "UserTbl", "LanID = '" & MyID & "'") & "\" & strFileName
if len(dir(strClient)) <> 0 then
kill strClient
end if
filecopy strMaster, strClient
The dlookup in the strClient simply looks up the path where the user opened up the instance of the client mdb. (I record this on every instance).
My issues is that I am not getting consistent results. Sometimes it will copy the file, and sometimes it won't. I've changed it to run from a button on the Installer Form, and it works every time, so I'm guessing it has something to do with timing.
I've tried putting a pause function before the kill command, and that seems to help if I set the pause to 3 or 4 seconds. I originally used Shell to open the Installer, but got rid of it as I heard that it was running concurrently with the installer.mdb code.
I'm thinking it's something obvious, but I've been staring at this for about an hour and can't figure it out. Ideally, I don't want the user to interact with this form using an "Install" button, but would like it to happen in the background. i.e. I want to set the .visible = false at some point.
Can anyone see an issue with this method, or suggest a better method to push out new copies of the client - and I can't use any EXE install programs.
You likely want to use the shell() command. You can use CreateObject, but that feature is NOT available in the runtime. (and worse, it will not work).
The simple trick is ONCE you shell out to the upgrade program, you want to QUIT the main program (since you can’t copy over it while it is running).
The code I use is thus:
strCurrentDir = CurrentProject.Path & "\"
' path to msaccess.exe is determined here
strShellProg = q & SysCmd(acSysCmdAccessDir) & "msaccess.exe" & q & " "
' path to current dir...assume upgrade program in same folder
strShellProg = strShellProg & q & strCurrentDir & "UpGrade.accDE" & q
If Shell(strShellProg, vbNormalFocus) > 0 Then
Application.Quit
Else
MsgBox "Un able to run upgrade", vbCritical, AppName
Application.Quit
End If
In above “q” is = """" (a single double quote).
So you shell out, and then immediate do a application.Quit. The upgrade program should have a “prompt” like “about to upgrade – ok”. That “prompt the user has to hit gives the main application time to quit. I also useally write out the locations to a text file in the above code - but the above steps to shell() is the main takeaway solution here.
As noted, you can use create object, but THEN if you quit the main application, then that variable holding the upgrade program will also go out of scope (or worse, the main application cannot shut down because it is “hosting” an automated copy of the upgrade application by CreateObject.
So you in practical terms don’t want to use createObject, since this means the main program is “hosting” or “holding” a copy of the upgrade program and the main program really can’t quit.

.bat file - How to start a .bat file when I rename any file in a specific folder

I want my .bat file to run after I rename a file in the Sources folder that is located here:
C:\Users\UserName\Videos\Gameplays\HeroesOfTheStorm\Sources\
The .bat file is located in the same Sources folder.
How can I do that without double-clicking on the .bat file manually? I want it to run automatically after I rename a file in the Sources folder.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set MonitoredEvents = WMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""C:\\\\Users\\\\David Candy""'")
Do
Wscript.Echo MonitoredEvents.NextEvent.TargetInstance.PartComponent
WshShell.Run "cmd /c ""C:\folder\batchfile.bat""", 1, false
Loop
Note the use of 4 \ for 1 in directory name but nowhere else.
It a vbs file. It monitors a directory and will run commands if you rename or create files in that directory. WITHIN 10 means it tests every 10 secs.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set MonitoredEvents = WMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""C:\\\\Users\\\\David Candy""'")
Do
WMIPath = Split(MonitoredEvents.NextEvent.TargetInstance.PartComponent, "=")(1)
FilePath = Replace(WMIPath, "\\", "\")
WshShell.Run "cmd /k echo File Renamed is " & FilePath & "&" & Filepath
Loop
Ok, you can do this in a rather complicated way with windows tools.
First, you need to enable Auditing.
run GPEDIT and then go to: Computer Configuration --> Windows Settings --> Security Settings --> Local Policies --> Audit Policy --> Audit object Access
enable i guess both success and failures.
Now go to the folder you want to monitor, double-click and go to: security tab --> Advanced --> Auditing Tab
add a rule with your user (or a groups or something like that) that will make a log for the rename (the is no rename event, but you can try different combination, like file creation, file delete or read/write attributes you can find)
after that, now if you got to Event Viewer, Security log, a couple of events with Ids 4565/4663 will appear when the operation you selected is perfomed.
last thing, open the Task Scheduler and create a new task, with the activation set to the trigger of the event and an action that will run the bat.
Some of the term may differ, my Windows is not in English so i might have translated something wrong. Also, you might do some tests to see if you have everything set up correctly.
Another option might be setting up a small application that monitors the folder and run the bat accordingly. You can do such a thing in Java or in other languages.
This can be achieve easily with AutoHotkey using WatchDirectory()
https://stackoverflow.com/a/30582696/883015

Run batch silently by hotkey (permissions issue?)

I am trying to control Spotify volume from inside a game without switching windows. To do this I did the following:
A. Found nircmd.exe from NirSoft that can control individual program volumes in Windows
B. Wrote a batch to execute the command
C. Wrote this .vbs script to hook into the batch silently:
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "Spotify App Volume Up.bat" & Chr(34), 0
Set WshShell = Nothing
^
Using this .vbs snippet to accomplish silent run is really popular. This works when I click the .vbs myself. The problem I have is I need a way to execute this from another program. When I try to execute from any hotkey program or something like AutoHotKey it gives me this error:
ActiveX Component cannot create object 'WScript.Shell' Code: 800a01ad
I tried setting security of all involved files to everyone, full control. It did not help. It seems to me that Windows is trying to protect me by preventing scripts from initializing by other programs. Any suggestions? Otherwise does someone know how to execute this silently with another method?
EDIT:
AutoHotKey's run command will do this:
run "mybat.bat",, Hide
However, does anyone know how to get around this behavior with .vbs? I am curious.
I believe all your scripts, vbscript and your batch file can potentially be written natively in Ahk, but here's how I'd launch your vb script.
x:: ; press x
sc := ComObjCreate("ScriptControl")
sc.Language := "VBScript"
script =
(
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "Spotify App Volume Up.bat" & Chr(34), 0
Set WshShell = Nothing
)
sc.ExecuteStatement(script)
return

Launch two Explorer windows side-by-side

Is there a way to launch two Explorer windows side-by-side (vertically tiled) with a Batch script?
If not, how might I do this with VBS?
I have modified the VBS script above by Hackoo to do exactly what the OP wants...
The comments in the script explain exactly what it will do.
If the two windows don't set into correct position, increase the 'Sleep' time and try again.
If you want a horizontal split, use 'objShell.TileHorizontally'.
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Launches two Explorer windows side-by-side filling the screen dimensions.
''' Minimizes all current open windows before launch; if this is not done,
''' the current open windows will also be resized along with our two windows.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Calc,AppData,objShell
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
Call Explore(Calc)
WScript.Sleep 800
Call Explore(AppData)
WScript.Sleep 800
objShell.TileVertically
Set objShell = nothing
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
This might be in the same category as your question. :)
How can a batch file run a program and set the position and size of the window?
Unfortunately it seems that its not possible without any external third part software in batch. Probably easier in VBS - if so the answer should be in the link.
Try this code :
Option Explicit
Dim Calc,AppData
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Call Explore(Calc)
Call Explore(AppData)
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************

Running a Batch File in the Same Directory from the VBS

I have a VBScript that is supposed to run a .bat or .vbs file, but it doesn't work!
It comes up with an error saying that the file could not be found, whether i put in a file path or not (it shouldn't matter anyway I think because it's in the same directory).
So my question is, how do I start a .bat file (or even better, a .vbs file) from within a VBScript?
The relevant code is bellow:
'*******This is the start of my open command that doesn't work*******
Do
If Hour(Now) >= 9 And Hour(Now) <= 18 And Minute(Now) = 34 And Second(Now) = 59 Then
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "F:\\EAS\Volume Up.vbs"
Set shell = Nothing
MsgBox "My De-bug Message Box which doesn' even get to open"
WScript.Sleep 2000
Set WshShell = CreateObject("WScript.Shell")
music = "C:\...\MYFILE.wav"
WshShell.Run "wmplayer """ & music & """", 0, True
WScript.Quit 1
Else
'*******This is the end*******
So what am I doing wrong? Is it the wrong way to open it? What should I put instead?
It would be good to see the contents of the .BAT File.
Your code seems fine as I am able to run the below script on my machine:
dim shell
set shell=createobject("wscript.shell")
shell.run "tester.bat"
You may not see what the .BAT File is doing as it happens so quickly, as a tester add the following command to the end of your .BAT Script:
pause
Then you will see the command prompt open. As per my VB code above, the .BAT file contents are below:
#echo OFF
#echo %time%
pause
This will show you the current time and then pause, leaving the command prompt open. Give this a go as a tester as it works fine for me.
putting triple quotes (as suggested by ToThePoint) around the path solved my vbs file error, where it was failed to find the file on specified path as file path was having spaces, like
D:\Main\My text Files\abc.txt.
thanks :)
The only thing i can think of is that it must be a typo in the file name.
Can you ensure the file name is spelled correct?
Or else please post the exact error you get.

Resources