http://www.perlmonks.org?node_id=35902
Category: NT Admin
Author/Contact Info tye
Description:

Cleans up the System and User-specific PATH persistent environment variables by removing duplicate and invalid entries. Can also add directories to the System PATH. Note that it has been wrapped by pl2bat for ease of use under Windows NT. This script does not work for Win9x. I've tested it under WinNT, Win2K, and WinXP.

Update: Now also fixes the value type having been set to REG_SZ instead of REG_EXPAND_SZ.

Update 2: Now also sets the PATH in the parent command shell, which can be very convenient. Code also cleaned up just a bit. See reply below for older version.

Update 3: (2007-01-20) Fixed but where "ARRAY(...)" written to change log rather than path strings, minor whitespace ajustments, and fix bug demerphq pointed out long ago.

@rem = '--*-Perl-*--
@echo off
if "%OS%" == "" goto Win95
perl -x -S "%0" %*
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
goto endofperl
:Win95
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
@rem ';
#!/usr/bin/perl -w
#line 13

use strict;

Main();
exit( 0 );

sub ExpandEnv {
    my( $str )= @_;
    while(  $str =~ /%([^\s=]+)%/  ) {
        my $repl= $ENV{$1};
        if(  ! defined( $repl )  ) {
            warn "%$1% not set in environment -- dropping.\n";
            return "";
        }
        $str =~ s//$repl/;
    }
    return $str;
}


sub CleanPath {
    my( $aPath, $hUser )= @_;
    my( $path, $dir );
    my @GoodPath= ();
    my %GoodPath= ();
    while(  @$aPath  ) {
        $path= shift(@$aPath);
        print STDERR qq<  "$path"- >;
        $dir= ExpandEnv( $path )
            or  next;
        $dir =~ s#([^:/\\])[/\\]$#$1#;
        print STDERR qq<is "$dir"; >
            if  $dir ne $path;
        $path =~ s#([^:/\\])[/\\]$#$1#;
        if(  ! -d $dir  ) {
            warn "does not exist -- dropping.\n";
        } elsif(  $dir !~ /^([a-z]:|\\\\)/i  ) {
            warn "isn't absolute -- dropping.\n";
        } elsif(  $GoodPath{uc $dir}  ) {
            warn "is a repeat -- dropping.\n";
        } elsif(  defined($hUser)  &&  $hUser->{uc $dir}  ) {
            warn "is user-specific -- dropping.\n";
        } else {
            if(  $path =~ /^\Q$ENV{SYSTEMROOT}\E/io  ) {
                $path =~ s/^\Q$ENV{SYSTEMROOT}\E/%SystemRoot%/;
                print STDERR qq<changed to "$path">;
            }
            warn "is good -- keeping!\n";
            push( @GoodPath, $path );
            $GoodPath{uc $path}= $path;
            $GoodPath{uc $dir}= $path
                if  $dir ne $path;
        }
    }
    @$aPath= @GoodPath;
}


sub SplitSysPath {
    my( $SysPath, @dirs )= @_;
    my @SysPath= split( /;/, $SysPath->[0], -1 );
    my $dir;
    foreach $dir (  @dirs  ) {
        if(  $dir !~ m#^[a-z]:[/\\]#i  ) {
            die qq<Usage:  $0 ["x:\\dir_to_add" [...]]\n>,
                "Cleans invalid and repeated directories from the syst
+em\n",
                "and user-specific PATH environment settings.\n",
                "Prepends any listed directories to the system PATH.\n
+";
        } elsif(  ! -d $dir  ) {
            die "No such directory ($dir): $!\n";
        } else {
            warn "Prepending directory ($dir) to system path.\n";
            unshift( @SysPath, $dir );
        }
    }
    return @SysPath;
}


sub SaveChanges {
    my( $keyEnv, $keyPath, $avPath, $type )= @_;
    if(     $keyPath->[0] eq join( ";", @$avPath )
        &&  $keyPath->[1] != REG_SZ()
    ) {
        warn "\u$type PATH required no changes.\n";
    } elsif(  @$avPath  ) {
        if(  $keyPath->[1] == REG_SZ()  ) {
            warn "\u$type PATH changed from REG_SZ to REG_EXPAND_SZ.\n
+";
            $keyPath->[1]= REG_EXPAND_SZ()
        }
        $keyPath->[0]= join( ";", @$avPath );
        $keyEnv->{"/PATH"}= $keyPath
            or  die "Can't set $type PATH in Registry: $^E\n";
        warn "\u$type PATH successfully updated.\n";
    } elsif(  "" ne $keyPath->[0]  ) {
        if(  ! delete $keyEnv->{"/PATH"}  ) {
            warn "Can't delete (now-useless) $type PATH ",
                 "from Registry: $^E\n";
        } else {
            warn "Now-empty $type PATH successfully deleted.\n";
        }
    }
}


sub SaveState {
    my( $SysPath, $UserPath )= @_;
    my $UserName= $ENV{USERNAME} || "user";

    if(  open( TEMP, ">> $ENV{TEMP}\\CleanPath.save" )  ) {
        printf TEMP "On %d/%02d/%02d %02d:%02d:%02d:\n",
            sub { $_[0]+=1900; $_[1]++; return @_ }
                ->( (localtime)[5,4,3,2,1,0] );
        print TEMP "Old system PATH=$SysPath\n";
        print TEMP "Old $UserName PATH=$UserPath\n";
        close TEMP;
    } else {
        warn "Can't write to $ENV{TEMP}\\CleanPath.save: $!\n";
        warn "Old system PATH=$SysPath\n";
        warn "Old $UserName PATH=$UserPath\n";
    }
}


sub SetParentPath {
    my( $path )= @_;

    my $start= tell(DATA)
        or  die "Can't tell(DATA): $!";
    open DATA, "+< $0"  or  die "Can't read self ($0): $!\n";
    seek( DATA, $start, 0 )
        or  die "Can't fseek(DATA,$start,0): $!";
    die "Expected :endofperl after __END__ of $0.\n"
        unless  <DATA> =~ /^\s*:endofperl\s*$/i;
    seek( DATA, 0, 1 )
        or  die "Can't fseek(DATA,0,1): $!";

    if(  $path ne $ENV{PATH}  ) {
        warn "Updating current command shell's PATH...\n";
        print DATA "set PATH=$path\n";
    }

    truncate DATA, tell(DATA);
}


sub Main {
    my $Reg;
    use Win32::TieRegistry ( TiedRef => \$Reg,
        ArrayValues => 1, Delimiter => "/", ":REG_" );
    my $UserEnv= $Reg->{"CUser/Environment/"}
        or  die "Can't open Registry key, CUser/Environment/: $^E\n";
    my $SysEnv= $Reg->{
            "LMachine/System/CurrentControlSet/Control/"
        .   "Session Manager/Environment/"
    }
        or  die "Can't open Registry key, Session Manager/Environment:
+ $^E\n";
    my $SysPath= $SysEnv->{"/PATH"}
        or  die "Can't read system PATH from Registry: $^E\n";

    my @SysPath= SplitSysPath( $SysPath, @ARGV );

    my $UserPath= $UserEnv->{"/PATH"} || [ "", REG_EXPAND_SZ() ];
    my @UserPath= split( /;/, $UserPath->[0], -1 );

    SaveState( $SysPath->[0], $UserPath->[0] );

    warn "Cleaning user-specific PATH:\n";
    CleanPath( \@UserPath );

    my %UserPath= map {uc $_, $_} @UserPath;
    warn "Cleaning system PATH:\n";
    CleanPath( \@SysPath, \%UserPath );

    SaveChanges( $SysEnv, $SysPath, \@SysPath, "system" );
    SaveChanges( $UserEnv, $UserPath, \@UserPath, "user-specific" );

    my $path= join ";", map { ExpandEnv($_) || () } @UserPath, @SysPat
+h;
    SetParentPath( $path );
}

__END__
:endofperl
Replies are listed 'Best First'.
Re: CleanPath
by demerphq (Chancellor) on Sep 05, 2003 at 16:41 UTC

    Minor nit around the %SystemRoot% handling. First off why does this only occur with %SystemRoot% is there something special about this variable that I am unaware of? Or does the OS do the correct handling if any %Env% vars are in the path? But more importantly there is a minor bug in that if two identical paths get a %SystemRoot% translation then they arent treated as dupes and both are kept. The following patch resolves this.

    --- cleanpath.orig.pl 2003-09-05 18:32:14.000000000 +0200 +++ cleanpath.pl 2003-09-05 18:32:43.000000000 +0200 @@ -59,7 +59,7 @@ } warn "is good -- keeping!\n"; push( @GoodPath, $path ); - $GoodPath{uc $path}= $path; + $GoodPath{uc $path} = $GoodPath{uc $dir} = $path; } } @$aPath= @GoodPath;

    As I mentioned in the CB im going to use this as a base for capturing %ENV changes after running utility scripts like VCVARS32.BAT and VSVARS32.BAT and committing their changes to the default system enviornment. Thanks a lot, ive been wanting to write the script youve posted and the extension I mention for a while. Now i only need to the latter.

    Cheers.

    Oh, and I really do think the use Tie::Registry bit should go at the top of the file. Miaow. ;-)

    Update

    This is a hack i did of tyes code. It runs a batch file and then extracts the info out and compares it to the current enviornment. Any changes are written into the SYSTEM enviornment registry data.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      First off why does this only occur with %SystemRoo­t%

      Other environment variables can be put in path components and CleanPath will handle them just fine. What it does special with %SystemRoot% is that if you put a directory in your $ENV{PATH} that is a subdirectory of %SystemRoot% without saying "%SystemRoot%", then CleanPath changes it to use %SystemRoot%. So "CleanPath C:\Windows\system32\trojans" will actually put "%SystemRoot%\trojans" into your path.

      This feature was mostly added because the big motivation for writing this script was that a coworker had written a script to flush $ENV{PATH} changes into the registry and done a bad job of it, not realizing that $ENV{PATH} doesn't match what gets put in the registry for several reasons (notably user-specific path components and expansion of %SystemRoot%) so I wanted to clean this mess up easily (quite a few system had been corrupted by this before anyone noticed). If you use %SystemRoot%, then those entries in your $ENV{PATH} continue to work even if the drive letter for that partition changes on your when you reboot (and I was working in situations where this wasn't that uncommon of a situation); that's why those entries are done that way.

      I wasn't too worried about the bug you pointed out because just running CleanPath twice takes care of it. I didn't include your fix at first because I needed to look over how I was using %GoodPath to convince myself that there were no problems with that fix. I just now incorporated your fix (slightly differently) as I was making some other minor updates. Thanks!

      - tye        

        If you copy the code be sure not to miss the last "h" its on a newline +h ...:  my $path= join ";", map { ExpandEnv($_) || () } @UserPath, @SysPat i think the last should read @SysPath? Thanks MH

Re: CleanPath
by Anonymous Monk on Oct 25, 2011 at 13:39 UTC

    It appears to be common to split on ; and your program is no exception.

    It doesn't appear to handle quoted path names, which are valid, both the "pa;th" and "pa th"\"of space" variety

    shell commands (copy paste into cmd.exe):
    md "pa;th" echo @echo how the pa;th are ya > "pa;th"\pathme.bat pathme set path="pa;th";%path% pathme md "pa;th"\"of space" echo @echo how the pa;th\of space are ya > "pa;th\of space"\pathem.ba +t pathem set path="pa;th"\"of space";%path% pathem
    Output:
    C:\>md "pa;th" C:\>echo @echo how the pa;th are ya > "pa;th"\pathme.bat C:\>pathme 'pathme' is not recognized as an internal or external command, operable program or batch file. C:\>set path="pa;th";%path% C:\>pathme how the pa;th are ya C:\>md "pa;th"\"of space" C:\>echo @echo how the pa;th\of space are ya > "pa;th\of space"\pathe +m.bat C:\>pathem 'pathem' is not recognized as an internal or external command, operable program or batch file. C:\>set path="pa;th"\"of space";%path% C:\>pathem how the pa;th\of space are ya C:\>
Re: CleanPath (updated)
by tye (Sage) on Jul 28, 2003 at 19:13 UTC

    I've made some improvements to this code. The old version is included here inside CODE tags inside of an HTML comment READMORE tags, so you can use "download code" if you want to see the old version.

                    - tye