Archive for 'Windows'

How to diff Word documents

It’s fairly straightforward to diff or compare different revisions of an Office document on Windows. The approach below applies to Word, Excel, and PowerPoint files, as well as to ones created by Lotus Symphony, OpenOffice.org, or LibreOffice.

  1. Download and install WinMerge. This is a free, open source utility.
  2. Download the xdocdiff plugin. Unzip it somewhere.
  3. Copy xdoc2txt.exe and zlib.dll to C:\Program Files\WinMerge
  4. Copy amb_xdocdiffPlugin.dll to C:\Program Files\WinMerge\MergePlugins
  5. Start WinMerge.
  6. Go to Plugins > List and check [x] Enable plugins.
  7. Go to Plugins and set it to [x] Automatic unpacking
  8. Close or restart WinMerge

You should now be able to select any two documents that you want to compare, right-click on them, and choose WinMerge to get a meaningful comparison of the textual differences between them.

If you are seeing line noise in the comparison, you need to make sure you enable the settings mentioned in steps 6 and 7 above.

Batch file look-up table for month names

The look up table goes in months.bat:

:: one record per line
:: = is the delimiter
@echo 1=jan
@echo 2=feb
@echo 3=mar
@echo 4=apr
@echo 5=may
@echo 6=jun
@echo 7=jul
@echo 8=aug
@echo 9=sep
@echo 10=oct
@echo 11=nov
@echo 12=dec

Given a month number, you can then look up the month abbreviation as follows:

:: Enable numeric comparison in if statements (Windows NT or better)
@SETLOCAL ENABLEEXTENSIONS

:: Set the month number you want to look up
:: 8 is August
@SET MN=8

:: Look up the months abbreviation (e.g. 8 is aug, 9 is sep, 10 is oct)
@FOR /F "tokens=1,2 delims==" %%i IN ('months.bat') DO @IF %%i EQU %MN% SET MW=%%j

:: The month abbreviation is now the %MW% variable
@ECHO The month abbreviation corresponding to %MN% is %MW%.
  • ‘months.bat’ executes that file and pipes the output to the FOR /F command
  • delims== sets the delimiter to the equals sign
  • tokens=1,2 means you want the first two tokens (e.g. 1 and aug for 1=aug=August)
  • %%i means the first token will go in %%i, second in %%j, etc

Alternate solutions are welcome. I’m still a bit unclear on the difference between %%i, %i, and %i%.

Useful links:

CCleaner utility for Windows

CCleaner is one of my favourite utilities for Windows. It does several things very well.

CCleaner ScreenshotOne of them is removing the temporary files that various applications leave lying around. For example, the Windows temporary files directory gets filled up with various installers that have a tendency to never be removed as well as fragments of files left open during crashes. For another, Windows retains the option to let you uninstall every patch Microsoft issues. This may be useful in a mission-critical server situation, but on a regular machine it just wastes space.

I just had to clean up a very space-constrained virtual machine for work. CCleaner instantly freed up 1.4GB of space simply by removing crud left around by installers and other applications.

CCleaner also does a beautiful job of correcting Registry inconsistencies, finding dead Start Menu shortcuts, and various miscellaneous tasks like free space management and cleaning up the Add/Remove programs list.

Creating Start Menu shortcuts with Javascript

While preparing the installer for the Web 2.0 Starter Toolkit for IBM DB2, I had to set up Start Menu shortcuts. The way to do that is to work through the Windows Scripting Host (WSH).

The WSH supports two built-in languages – VBScript and Jscript – and a theoretical number of third-party alternatives. VBScript is the better documented of the two in terms of examples, but I find its syntax ugly and constrained. Fortunately, Jscript can do anything VBScript can.

So here’s how we can create Start Menu shortcuts with Javascript.

Locate the Start Menu Programs folder

Most interesting Windows folders can be found by working with special folders

var shell = new ActiveXObject("WScript.Shell");
var startmenu = shell.SpecialFolders("Programs");

The shell object will be reused in code below, but you can redeclare it every time if you like.

Create a Folder

Scripting Guy has more details

var folder = "My App";
var group = startmenu + "\\" + name;

var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FolderExists(folder)) fso.CreateFolder(folder);

startmenu is defined above.

Create an LNK Shortcut

The very hidden file extension of standard Windows shortcuts is LNK. This distinguishes them from website shortcuts, which have the equally hidden extension of URL.

If you are interested in something closer to the symbolic links of Unix, the NTFS equivalent is called junctions. You may find Junction Link Magic of interest.

var name = "My Shortcut";
var file = "myfile.txt";
var path = "C:\\Program Files";

var shortcut = shell.CreateShortcut(group + "\\" + name + ".lnk");
shortcut.TargetPath = path + "\\" + file;
shortcut.WorkingDirectory = path;
shortcut.Save();

See the full list of properties. I recommend always setting the working directory for application links. If you don’t, your program won’t be able to load resources from it’s installation folder.

shell and group are defined above.

Create a URL Shortcut

This is very similar. The main difference is that you set the extension to URL.

var name2 = "My Other Shortcut";
var address = "http://example.org/";

var shortcut = shell.CreateShortcut(group + "\\" + name2 +".url");
shortcut.TargetPath = address;
shortcut.Save();

See the full list of properties.

shell and group are defined above.

Locate Program Files

When creating shortcuts, it is often useful to know where a user’s Program Files directory is located. It is called different things in different versions of Windows, and some advanced users like to move it or rename it.

var REG_PF = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir";

var progFiles = null;
var process = shell.Environment("PROCESS");
if (process)
	progFiles = process("ProgramFiles");
if (!progFiles)
	progFiles = shell.RegRead(REG_PF);

shell is defined in the first code excerpt.

Utilities for Windows I

Hard Drive Cleanup

WinDirStat is an excellent utility for visualizing your used disk space and drilling down to files that you can clean up, back up, and delete. It’s similar to SequoiaView and Steffen Gerlach’s Scanner, but even more powerful. You can drill down through a folder tree as well as highlight filetypes in the graphical representation. The next time my hard drive gets too full, this is the utility I’ll use to clean things up.

WinDirStat

It’s only weakness is that you can’t drill down in the visual representation once it’s built, unlike both SequoiaView and Scanner.

Scanner 0Scanner 1Scanner 2

Startup Management

AutoRuns is a very comprehensive utility for dealing with the bane of hidden startup programs. TSRs and daemons date back to prehistory, but starting with Win95 a lot of rude programs began adding themselves to the system tray. This utility exposes all of the different hooks for management.

Autoruns

Task Management

Process Explorer can:

  • Kill processes Task Manager cannot
  • Find which process is preventing you from deleting a file
  • Match up processes to windows
  • Kill the root process of a multi-process app

Process Explorer

I find it invaluable on any Windows install.

Defragmentation

As you delete files from your hard drive, gaps of empty space form. New files get split into parts across these gaps. Over time, performance degrades. Some file systems are less vulnerable to this than others, but both NTFS and FAT are affected.

Windows comes with a defragmenter, but it has stayed uniformly crappy and slow since 95. I vastly prefer Vopt. Unfortunately, it’s shareware. It’s also well done, informative, and very fast.

Is there a good, free alternative?

Vopt

Gods, I need to defragment!

Hidden Settings

Tweak UI is a cliché choice, but I don’t know of a good replacement. My favourite tweak is to disable everything except Text Document from the New submenu in Explorer. I only ever create Folders and Text Documents that way, and Explorer significantly delays the display of the menu until all the useless icons are loaded.

Tweak UI

While you are there, grab Create Command Window Here and Power Calculator. Being able to open a command prompt window by right-clicking on a folder is very convenient. Meanwhile, Power Calc has a usable history, saveable formulas, primitive graphing, and super-handy unit conversions.