Small script request (shrotcut target different depending on time of day)

Soldato
Joined
2 Jun 2004
Posts
18,423
Hey guys.

I was wondering if anyone who knew programming could write me a small script (Win7+Win10)?

Basically I have two near-identical folders each with an exe. And I want one shortcut on my desktop which, when clicked, will check the time of day, and if it's 8am-8pm it will launch the exe from folder "A", and if the time is outside of those hours it will launch the exe from folder "B".

Hopefully that's pretty simple (I honestly have no idea, please just ignore the request if it's a lot to ask!).

Cheers.
 
Last edited:
Not sure why you can't have 2 shortcuts but this bit of javascript would do it. Save with a .js extension...

Code:
var WshShell = new ActiveXObject('WScript.Shell');
var hour = (new Date()).getHours();

if (hour >= 8 && hour < 20)
   WshShell.Run("notepad.exe");
else
   WshShell.Run("mspaint.exe");


If you want to specify a full path including folders, you must use double backslash as separators eg "c:\\path\\to\\app.exe"
 
Ok my bad. Heh *facedesk*. It works fine. ;)

Although... having said that, I'm still having trouble with the paths. I use double backslashes as mentioned, but get the following error;

Code:
Line: 7
Char: 4
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)


The path is definitely correct. Example;
Code:
WshShell.Run("C:\\Program Files\\example\\blah.exe");


What could be causing the problem?
 
I see, you could try changing the current working directory of your script to the folder, and then execute the files directly to see if that may work so the script would become:

Code:
var WshShell = new ActiveXObject('WScript.Shell');
WshShell.CurrentDirectory ="C:\\Program Files\\example";
var hour = (new Date()).getHours();
if (hour >= 8 && hour < 20)
   WshShell.Run("blah.exe");
else
   WshShell.Run("blah2.exe");

I have just reused the script you already have and have not tested the above either. Just found that someone else online experienced a somewhat similar issue which was resolved in this manner.
 
Thanks a lot for the help.

Forgive my noobishness, but wouldn't that method depend on both exe files being in the same folder?
If so, what I'm trying to do makes that impossible.
 
Thanks a lot for the help.

Forgive my noobishness, but wouldn't that method depend on both exe files being in the same folder?
If so, what I'm trying to do makes that impossible.

You'd just define the current directory immediately before calling the exe.

Code:
var WshShell = new ActiveXObject('WScript.Shell');
var hour = (new Date()).getHours();
if (hour >= 8 && hour < 20)
{
WshShell.CurrentDirectory ="C:\\Program Files\\example";
  WshShell.Run("blah.exe");
}
else
{
WshShell.CurrentDirectory ="C:\\Program Files\\example2";
   WshShell.Run("blah2.exe");
}

I think that works - I don't use js much.
 
Could try with Powershell if you can't get above going?

Code:
$FilePath1 = "C:\Windows\System32\Calc.exe"
$FilePath2 = "C:\Windows\System32\mspaint.exe"
$CurrentHour = Get-Date -Format HH

If (($CurrentHour -Ge '08') -And ($CurrentHour -Le '20'))
{
    Write-Host "Running: $FilePath1"
    & $FilePath1
}
Else
{
    Write-Host "Running: $FilePath2"
    & $FilePath2
}
Pause

Save that in a .ps1 file. Should be able to right-click and run in powershell.
Or create a .bat file and stick the following in it to call the script:
Powershell.exe -ExecutionPolicy RemoteSigned "C:\Temp\runexe.ps1"

edit: oops, named the correct file - would have been calling itself!
 
Last edited:
Back
Top Bottom