My cmd.exe Profile

I wanted to set up a cmd.exe environment similar to my msys .bashrc for msysGit (, to set up environment variables and doskey macros (aliases.)

This post will show you some techniques for doing this.

As Larry Wall said, "it's easier to port a shell than a shell script."

I use Console2 for my various command prompts (msysgit, powershell, cmd.exe...) you can get it here:

First, how do we tell cmd.exe to run a script on startup? There are two ways, the way I use is with the /K option to run a batch file and continue. Another option is to set a registry key, see cmd /? for what registry key to set.

Make a file called .cmd_profile.cmd in your %HOME%, then configure your cmd.exe tabs in Console2 to something like this:

c:\windows\system32\cmd.exe /K c:\Users\rkitover\.cmd_profile.cmd

Or make a shortcut on your desktop with this command line.

Or set that registry to the location of your profile script.

My .cmd_profile.cmd looks like this:

@echo off

REM Presumably we will only use real consoles with this profile.
REM And perl readline only works with TERM=dumb .
REM Set this to 'cygwin' to work with cygwin utilities.
set TERM=dumb

doskey use_strawberry64=^
    set PATH=c:\strawberry\perl\site\bin;c:\strawberry\perl\bin;c:\strawberry\c\bin;%PATH%^
    $Tcall remove_path_dups^
    $Tcall strawberry_maint_macros

doskey use_activeperl64=^
    set PATH=C:\Perl64\site\bin;C:\Perl64\bin;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64;%PATH%^
    $Tcall remove_path_dups^
    $Tset LIB=C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\x64;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64^
    $Tcall activestate_maint_macros

doskey use_activeperl32=^
    set PATH=C:\Perl\site\bin;C:\Perl\bin;c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE;c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin;c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools;c:\Windows\Microsoft.NET\Framework\v4.0.30319;c:\Windows\Microsoft.NET\Framework\v3.5;c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\VCPackages;c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools;c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin;%PATH%^
    $Tcall remove_path_dups^
    $Tset LIB=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib^
    $Tset LIBPATH=c:\Windows\Microsoft.NET\Framework\v4.0.30319;C:\Windows\Microsoft.NET\Framework\v3.5;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\LIB^
    $Tcall activestate_maint_macros

call exec_macro use_activeperl32

Some features to notice here:

line continuation

The line continuation and escape character for certain shell metacharacters in batch files is ^.

doskey command separator

The command separator in doskey macros is $T, see doskey /? for more information.

environment variable expansion

Environment variables are expanded immediately when the batch file is executed, not when the command using them runs. Here the %PATH% I use is the %PATH% my cmd.exe starts with, always. This is not what happens in my .bashrc, but it suits my purposes in this case well enough that I haven't fixed it yet.

There are a couple of supporting .cmd utilities to go along with this profile. I put them into C:\Users\rkitover\bin and put that directory in my %PATH%.

@echo off

for /f "usebackq tokens=*" %%x in (`doskey /macros ^| perl -ne "next unless s/^%*=//; s/\s*\$T/\n/g; print"`) do %%x
@echo off

for /f "usebackq tokens=*" %%x in (`perl -le "for (split /;/, $ENV{PATH}) { $p{$_} || do { $p{$_}++; push @p, $_ }; } print join q{;}, @p"`) do set PATH=%%x
@echo off

doskey pclean=perl Makefile.PL$Tdmake realclean$Tdel /F MANIFEST

doskey pinst=perl Makefile.PL$Tdmake$Tdmake install$Tcall exec_macro pclean

doskey pprepare=rmdir /S inc $Tdel /F MANIFEST$Tperl Makefile.PL$Tdmake manifest$Tperl Makefile.PL$Tdmake

doskey pdist=call exec_macro pprepare$Tdmake dist

doskey pupload=call exec_macro pprepare$Tdmake upload
@echo off

doskey pclean=perl Makefile.PL$Tnmake realclean$Tdel /F MANIFEST

doskey pinst=perl Makefile.PL$Tnmake$Tnmake install$Tcall exec_macro pclean

doskey pprepare=rmdir /S inc $Tdel /F MANIFEST$Tperl Makefile.PL$Tnmake manifest$Tperl Makefile.PL$Tnmake

doskey pdist=call exec_macro pprepare$Tnmake dist

doskey pupload=call exec_macro pprepare$Tnmake upload

These use perl, I'll fix them to use something native like powershell or wsh at some point, but I don't know those languages yet.

Last modified: 2017-1-8 (日) at 9:14 pm