in reply to Change Working Directory in Windows!

Use chdir.

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

The argument can be

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.

Replies are listed 'Best First'.
Re^2: Change Working Directory in Windows!
by Jenda (Abbot) on Jan 22, 2010 at 10:41 UTC

    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?)