devgoddess has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have a rather odd question, I'm afraid, and I haven't been able to really find any information pertaining to it. I have written a script that uses a hard-coded directory path, the Windows system directory, to locate a file to modify. On some of my client's servers, the system path is under c:\WINDOWS, but on other servers it's under c:\WINNT. I would think the easiest way to handle this discrepancy would be to use the %SystemRoot% variable under Windows. However, I'm afraid Perl sees it as a hash. I get a syntax error on that line.

Anyone know how to force a Perl script to use the Windows default system path?

Dev Goddess
Developer / Analyst / Criminal Mastermind

"Size doesn't matter. It's all about speed and performance."

  • Comment on Using Window System Path Variable in Perl Script

Replies are listed 'Best First'.
Re: Using Window System Path Variable in Perl Script
by NetWallah (Canon) on Mar 14, 2004 at 23:55 UTC
    perl provides the entire collection of environment variables in the %ENV hash.

    You can get the scalar containing the windows path by indexing it thus:

    my $winpath = $ENV{windir}; # Returns C:\WINDOWS or whatever #If you prefer systemroot , my $sysroot=$ENV{SystemRoot};
    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
      Oh wow! That's cool! I didn't even know that existed. I read about the %ENV hash in several different books, but none of the books I have mentioned the windir key. Go figure. :-\

      THANK YOU!!!

      Dev Goddess
      Developer / Analyst / Criminal Mastermind

      "Size doesn't matter. It's all about speed and performance."

        At the command prompt in Windows, you can type the command "set" to see all your environment variables.
          A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Using Window System Path Variable in Perl Script
by bart (Canon) on Mar 15, 2004 at 01:46 UTC
    As an alternative to the environment variable, there's a Win32 API call you can use: GetWindowsDirectory, and there's also its sibling GetSystemDirectory. You can call these by using the Win32::API module.
    # Old Skool calls use Win32::API; my $getwindir = new Win32::API(kernel32 => GetWindowsDirectoryA => [qw +(P N)], "N") or die "Can't locate function: $!"; # ... or use 'PN' for the 3rd parameter, if Win32::API is recent enoug +h (version?) my $buffer = "\0" x 300; my $length = $getwindir->Call($buffer, length $buffer); print substr $buffer, 0, $length;
    The following crashes for me, if I don't delete $func->{proto}... Surely that must be a bug? Otherwise, I must be misunderstanding something really badly...
    # New Skool calls ($Win32::API::VERSION >= 0.40) use Win32::API; my $getwindir = new Win32::API(kernel32 => 'LONG GetWindowsDirectoryA( + LPSTR buffer, LONG maxlength )') or die "Can't locate function: $!"; # Do this, or bear the consequences: delete $getwindir->{proto}; my $buffer = "\0" x 200; my $length = $getwindir->Call($buffer, length $buffer); print substr $buffer, 0, $length;
    And of course, you can replace "GetWindowsDirectory"/"GetWindowsDirectoryA" everywhere with "GetSystemDirectory"/"GetSystemDirectoryA", to get the Windows/System subdirectory
      Hey, that's pretty cool, too. Thanks for turning me on to the Win32::API module. I really didn't know it existed. (Either that, or I probably just never noticed it before.) I'm coding exclusively in Windows 2K Server, and my scripts run exclusively on Windows machines, so this will be a good module to know and study.

      As for my purposes with this script, I went ahead and used the $ENV{windir} variable. It took one line of code to accomplish what I wanted. Tight is good. ;)

      Thanks a lot for the reply. I love the way I'm constantly picking up new info and techniques around here.

      Dev Goddess
      Developer / Analyst / Criminal Mastermind

      "Size doesn't matter. It's all about speed and performance."