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

I have a Win32 script that prompts the user for configuration information the 1st time it's invoked. Once the user enters this information, a config.ini file is created to perpetually store the data. I'm writing it with this:
open (INI, '>config.ini'); print INI "data:user_input"; close INI
Nothing special, right? Well, the INI file is created in the directory that the user was in at the time, not necessarily the same directory as the script. i.e. if the script is located at c:\scripts\script.pl and I make the following call from the D:\> prompt

D:\> c:\scripts\script.pl

the INI file is created in D:\ not c:\scripts

I know I could fix it by changing the OPEN command to
open (INI, ">".$scriptPath."\\config.ini"); print INI "data:user_input"; close INI
if I could get the path of the script into $scriptPath. However, I don't know how to do this. Doing a $dir = `cd`; returns the directory the script was called from. Any ideas?

Replies are listed 'Best First'.
Re: Script Directory vs. Directory Script was Called From
by maverick (Curate) on Aug 15, 2001 at 01:10 UTC
    Don't know if this module exists or works under Windows, but try:
    use FindBin; print $FindBin::RealBin,"\n";
    no matter where I ran the script from it always gave me the full path to where the script was located.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      Sure does work under Windows... sure does exactly what I wanted it to do! Thanks :-)
Re: Script Directory vs. Directory Script was Called From
by John M. Dlugosz (Monsignor) on Aug 15, 2001 at 01:40 UTC
    To populate $scriptPath, look at the script name which is in $0. Grab the path part off that.

    Or, being Windows, use the Registry either for the data itself or a single entry pointing to the INI file. Hard-code the registry path as HKCU\Software\YourBrand\YourScript.

    —John

Re: Script Directory vs. Directory Script was Called From
by tachyon (Chancellor) on Aug 15, 2001 at 03:57 UTC
    use Cwd; print "Current dir is ", getcwd, "\n"; (my $dir) = $0 =~ m|(.*?)[^\\/]+$|; print "Current dir is ", $dir, "\n";

    These both return the correct result but the format is a little different.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Script Directory vs. Directory Script was Called From
by runrig (Abbot) on Aug 15, 2001 at 04:09 UTC
Re: Script Directory vs. Directory Script was Called From
by foogod (Friar) on Aug 15, 2001 at 01:03 UTC

    use this....

    #!C:\Perl\bin\perl.exe use Cwd; $dir = getcwd; print "current dir = $dir";

    It worked right for me ... whatever directory you are in when you call the script, that is the directory that is delivered.

    HTH

    - f o o g o d

      ignore what i wrote ... I obviously didnt read your question close enough ... I did find that Maverick's comment worked correct. So .... its tuesday ... damn

Re: Script Directory vs. Directory Script was Called From
by abstracts (Hermit) on Aug 15, 2001 at 00:52 UTC
    Hello

    This is not the best idea but give it a shot. Cycle through all directories in your PATH environment variable and see in which directory your script resides. The first directory you find is the directory you want since this is what was used to invoke your script.

    Otherwise, put it in c:\windows!!! Sorry, a non-windows user.

    Aziz,,,