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

Heeeelp i've tryied many many ways..

can't manage to change the working directory.. I could print some of the things i've tryied.. but..

just tell me the rightt one pleeeaase. Thanks.

Replies are listed 'Best First'.
Re: Change Working Directory in Windows!
by jwkrahn (Abbot) on Jan 22, 2010 at 02:02 UTC

    Use Perl's chdir operator.    And don't forget to verify that it worked correctly.

Re: Change Working Directory in Windows!
by ikegami (Patriarch) on Jan 22, 2010 at 02:43 UTC

    Use chdir.

    chdir($dir) or die("Can't change to dir $dir: $!\n");

    The argument can be

    • A drive (e.g. F:)
    • A relative path without a drive (e.g. docs)
    • A relative path with a drive (e.g. F:docs)
    • An absolute path without a drive (e.g. \docs)
    • An absolute path with a drive (e.g. F:\docs)
    • A UNC path (e.g. \\tribble\share)

    If both a drive and a directory is provided, both the current drive and the current directory on that drive will be changed. This differs from the cd shell command which only does the latter.

    Both slashes (\ and /) can be used as a separator and to prefix a UNC path.

    Make sure to properly escape backslashes in the string literals that produce the paths if applicable.

      I would not call the \docs an absolute path under Windows. Only the F:\docs and the UNC is absolute. All the rest is relative to something. F: to the current directory on the F: drive, docs to the current directory on the current drive, F:docs to the current directory on the F: drive and the \docs to the current drive.

      In either the case it seems to me like the OP wanted to change the current directory of the process that started the script. Something like:

      c:\some\silly\directory> perl find_something.pl params blah blah blah, I found it. c:\other\dir>
      That's AFAIK not possible. Though at times would be nice.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        I would not call the \docs an absolute path under Windows

        Neither would I. That's why I didn't call it that. (Although I might under more casual circumstances.)

        In either the case it seems to me like the OP wanted to change the current directory of the process that started the script.

        Ah, could be. I assumed he tried system('cd') to change Perl's current directory. A very common mistake.

        You could achieve what you think the OP wants by using for to capture a string printed by perl, then using a pair of cd to switch to that directory (or is there a switch to change both the drive and dir now?)

Re: Change Working Directory in Windows!
by markkawika (Monk) on Jan 22, 2010 at 02:02 UTC
    Why don't you tell us some of the things you've tried?