Start-up Scripts

From Ben's Writing

Jump to: navigation, search

Autoexec.bat in NT

MS-DOS used to have a convenient start-up script system, similar to the UNIX world (and I use the word similar here in the loosest possible way). Unfortunately, in later releases of Windows 2K, XP and now Vista, the autoexec.bat is no longer functional, although it is still present—and if you look closely, in the right light, it even has some useful words of wisdom to share: "REM Dummy file for NTVDM". Fortunately, MS has not disabled NT's scripting capacities completely: it just moved them. They now reside in:

%SystemRoot%\system32\autoexec.nt

Note the nt extension change, to reflect the shiny new OS—what a wonderful new world.

If you can't—like me—get this to work for you on Vista, then there is another alternative: the registry. Using either regedit or reg (pick your own poison) find you way to either:

HKLM\Software\Microsoft\Command Processor\AutoRun

and/or

HKCR\Software\Microsoft\Command Processor\AutoRun

The former is for all users, while the latter is for the current user (i.e. you). The key is a string, so simply point it towards a batch file you'd like to have run when you launch a command window. In my case, I called it autocomp.bat and put it in a directory structure with all my scrips: C:\AutoRun.

C:\AutoRun
C:\AutoRun\Executables
C:\AutoRun\Scripts

If you want to get really fancy, you can put per user scripts in the user's profile as well:

%USERPROFILE%\AutoRun
%USERPROFILE%\AutoRun\Executables
%USERPROFILE%\AutoRun\Scripts

These would be called from the HKCR, while the former would then be caled from HKLM. Alternatively, you could have the code called from the HKLM key also check to see if the user's profile has the AutoRun folder and if it does, enumerate the Script folder.

Special Considerations

If you are using Visual Studio, or some other tool that uses cmd.exe make sure not to change the current working directory in your start-up script. If you do, then some tools will not work correctly, because they rely on being in that directory to function. If you must change the current working directory, then be sure to restore it after you are done.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox