Tuesday, September 9 2008, 10:10
Windows Vista and batch files: UAC changes the rules (Part 2 of 2)
In the previous entry of this series, we discussed about how Windows Vista forced a path change when running a batch file elevated with UAC enabled, which causes the current directory to be different from the batch file location, which creates some problems when running some external tools residing in the batch file directory. The solution I found was to change the current directory at the very start of the batch file execution using a simple sequence.
CD /D "%~dp0"
REM Insert your code here
Now, while these commands aren’t very long, the syntax isn’t exactly easy to remember (especially the %~dp0 part) and it is easy to forget to add them only to find out the batch file fails under Vista. The best way would be to have them added every time we create a new batch file.
Personally, I used to create new batch files using Explorer’s context-menu and by going to the new entry to create a “new text document”, which would give me a new .txt file. After renaming the extension to .bat, I would start working on it. The best option would then be to have an entry in this New menu to create a “new batch file”. While this isn’t exactly rocket-science, this kind of tricks and customizations can save you some seconds everyday, which after some months might have saved you a good number of minutes but more importantly a lot of frustration.
Explorer’s “New” context-menu, with our new batch file entry
To do so, open the “%windir%\ShellNew”, directory, which should already host some files which are used as a templates when creating the new files. Some file formats and applications expect files to have headers which are used by their applications’ at runtime to determine the validity of the opened file, which means we can customize the output of the “New” context menu command. I then created a file named “New.bat” in this directory and pasted the commands above.
That said, it isn’t enough for the new batch file option to appear in the New menu. Another step is required which is to register this new file to the registry, more specifically in the file extension’s key in HKEY_CLASSES_ROOT under the ShellNew sub-key. Here is an example in the .reg format which you can import directly.
If we go back to the “New” context menu, we find out that the option to create a new batch file is here and that clicking on it creates a file named “New MS-DOS Batch File.bat” with all our commands pre-recorded in it.Interestingly enough, we didn’t need to specify anything in the registry regarding the new file’s default name. Explorer is smart enough to figure out by itself by using the file extension’s description already present in the registry.
If the appearance of “MS-DOS” in the file name gives you shivers, you may prefer to use the .cmd extension instead which seems to be the extension for NT batch files, but to be honest I am not sure which differences in handling there are between both extensions, if there is any. Bat files have a much higher chance to be recognized on the 9x generation of the operating system, although if you use NT-specific batch-language extension like the ones we use to pre-populate our batch files, it will not work because they aren’t recognized by command.com, the original command-line interpreter. In my personal case, it is a long standing habit to refer as batch files as .bat and .cmd simply doesn’t have that ancestral sound to it… which to the (not-so?) old geezer I am whose first “programming” experience was with batch files, is sort of important.