2011-09-18

Create an MSIE Favorite using VBA & WSH

A little sub-routine to create MSIE Favorites for MS-Office users. It uses Windows Scripting Host (WSH) which has a shell object that has functions to find a computer's Favorites folder (SpecialFolders()) and create a URL shortcut (CreateShortcut()).


Option Explicit

'Add references:
'1. Microsoft Scripting Runtime (Scripting namespace)
'2. Windows Script Host Object Model (IWshRuntimeLibrary namespace)

Public Sub CreateFavorite(ByVal strName As String, ByVal strUrl As String)
  Dim fso As Scripting.FileSystemObject
  Set fso = CreateObject("Scripting.FileSystemObject")
  
  Dim wshShell As IWshRuntimeLibrary.wshShell
  Set wshShell = CreateObject("Wscript.Shell")
  
  Dim strFavorites As String
  strFavorites = wshShell.SpecialFolders("Favorites")
  
  Dim shortcut As IWshRuntimeLibrary.WshURLShortcut
  Set shortcut = wshShell.CreateShortcut(fso.BuildPath(strFavorites, strName & ".url"))
  shortcut.TargetPath = strUrl
  shortcut.Save
  
  Set shortcut = Nothing
  Set wshShell = Nothing
  Set fso = Nothing
End Sub

Note to myself: don't confuse:


  • IWshRuntimeLibrary.WshShell with Shell32.Shell in Microsoft Shell Controls and Automation
  • WshShell.SpecialFolders() with FileSystemObject.GetSpecialFolders() function. The latter function only returns a small number of special folders (windows, system and temporary).

2011-05-29

Avoid multiple instances of a task in Windows Task Scheduler

Using Task Scheduler, one way to run a task more often than once a day was to repeat the task within the day. For instance, if you wanted to run a task every hour, you would repeat the task once an hour for that day. Now, if the task's running time is longer (e.g. the task is delayed by a slow network connection) than the repeat interval then another instance would be started even if you specify Do not start a new instance!

For a backup task, my workaround is to specify an hourly repeat interval and call a wrapper (e.g. a CMD or VBS script) to test for a lock file before executing the main program. In the wrapper, if the lock file exists then the wrapper aborts else it creates a lock file, runs the main program then deletes the lock file.

It looks like a task's Do not start a new instance parameter only applies to triggers. With Task Scheduler 2.0 (Vista onwards), you can create triggers for specific times of the day (say 01:00, 02:00, etc. for hourly triggers) and only one instance of a task is started. It's a pity there aren't shorter trigger intervals because to schedule a task every hour of the day, you have to create 24 triggers.

2011-04-16

Running Batch Files in Vim Ex-Mode

To modify a file using vim in ex-mode, you should be able to do this: vim -esn < modify.ex File.txt. The -esn option starts vim in ex-mode, a non-interactive session and no backup is created. The modify.ex file can contain a sequence of editing commands, ending with wq to write and close the file. However, in a Windows cmd session, the editing commands in modify.ex are executed but vim doesn't exit.

My workaround is to run a command (-c option) which uses vim's source command to run modify.ex: vim -esn -c "source modify.ex" File.txt.

2011-03-26

Configure NetBeans to Statically Link MinGW C & C++ Libraries

Installed MinGW g++ 4.5.2 and got the following Windows error when trying to run a C++ program I just compiled: The program can't start because libgcc_s_dw2-1.dll is missing from your computer. After fixing that error, Windows displayed a similar message regarding libstdc++-6.dll. Basically, g++ doesn not statically link the C and C++ standard libraries with an executable by default.

  • In Windows, copy the required library files, libgcc_s_dw2-1.dll and libstdc++-6.dll from the MinGW bin folder into the same folder as the executable or add the MinGW bin folder into the PATH environment variable.
  • If using NetBeans 6.9, configure the project properties to statically link the libraries to the executable:
    1. Select menu item File, Project Properties.
    2. In the Project Properties dialog, expand node Build, Linker.
    3. In Command Line, set Additional Options = -static-libgcc -static-libstdc++