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

Hi,

I have the following requirement:

Do some work in any directory the codes are called. However, if the pwd is C:\, don't do the work there.

So, I've come out with the following program:

use Cwd; if (lc(cwd()) eq 'c:/') { print "I'm not working on this directory!\n"; } else { print "I'll work on this directory!\n"; } end;

However, there's a loophole here. If the user subst's C:\ with Z:\, and calls the codes from Z:\, it'll still run. It is not supposed to run, because Z:\ is effectively C:\.

Is there anyway to work around this subst, or even mapped drives?

Thanks in advance!

Replies are listed 'Best First'.
Re: Real Path Name
by bellaire (Hermit) on Mar 09, 2009 at 12:58 UTC
    It looks like Win32::FileOp exports the method Substed, which if invoked on your drive letter will tell you whether it's substed somewhere else, or where the drive mapping leads if it's a network mapped drive. The output syntax is a little odd (in particular it uses windows-native \ for directory separation rather than perl-standard /), but with a little work it should get the job done. Running the following in -e from the command line:
    print join(',',Substed(split(/\//,cwd)));
    ... yields ...
    C:\>perl -MCwd -MWin32::FileOp -e "..." ,HarddiskVolume1 X:\>perl -MCwd -MWin32::FileOp -e "..." \??\C:\temp, Z:\>perl -MCwd -MWin32::FileOp -e "..." \??\C:, O:\>perl -MCwd -MWin32::FileOp -e "..." ,LanmanRedirector\;O:000000000002ef75\SOME-MACHINE\Network\Share
    Note the comma in the above examples, Substed returns a list of two elements, which I joined with a comma in the output. I've substed X: to C:\temp, Z: to C:\, and O: is a network share.
      Thx a lot, bellaire! It seems that that is what I'm looking for, a way to determine if the cwd is a substituted / mapped, or "real" directory.
Re: Real Path Name
by jethro (Monsignor) on Mar 09, 2009 at 11:22 UTC

    Your script could refuse to work in any directory that has a file 'dontexecutehere.txt' in it. Then you should create that file in C:\, with a small text in it why it was created. I would do this as an additional precaution since there is the danger that someone else might remove the file out of ignorance

      Thx, Jethro. But the do_not_do_any_work directory is actually a regression test depository. I'm hoping that there could be a more foolproof method of handling it. Thx again!