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

Hi - I am using the below code to change the directory,for some reason the directory doesnot change,I dont see any error though,the directory remains the same.

print "CWD:$cwd"; my $path_data = $cwd."\\data";//Also want to make the concat in-cas +esensitive chdir($path_data) or die "Cant chdir to $path_data $!"; print "\nCWD:$cwd"; //same directory as above gets printed

Replies are listed 'Best First'.
Re: chdir not working
by GrandFather (Saint) on Mar 14, 2011 at 21:26 UTC

    The code you have given is not Perl. // is not a valid Perl comment. Have you adapted this code from a different language perhaps? How about you show us some real code you have run and that demonstrates the problem you are having. Mean time, the following may be the Perlish version of what you are trying to do:

    use strict; use warnings; use Cwd; print cwd(); my $path_data = cwd () . "\\data"; #Also want to make the concat in-ca +sesensitive chdir($path_data) or die "Cant chdir to $path_data $!"; print "\n", cwd();
    True laziness is hard work
Re: chdir not working
by toolic (Bishop) on Mar 14, 2011 at 21:22 UTC

    The directory probably is changing, but you are printing a stale value. You did not change the value of the $cwd scalar variable between print's. If you are using Cwd, try this:

    print "CWD:$cwd"; my $path_data = $cwd."\\data";//Also want to make the concat in-casese +nsitive chdir($path_data) or die "Cant chdir to $path_data $!"; $cwd = getcwd(); print "\nCWD:$cwd"; #//same directory as above gets printed

      Actually I want to go one level back and then append data like below,but seems like the syntax is not right,it nots going one level up.Can you advise how can I do that? Cant chdir to E:/qdepot_automation/files\\data No such file or directory at perl.pl line 157

      print "CWD:$cwd"; my $path_data = $cwd."..\\\\data";//Also want to make the concat in-ca +sese +nsitive chdir($path_data) or die "Cant chdir to $path_data $!"; $cwd = getcwd(); print "\nCWD:$cwd"; #//same directory as above gets printed
Re: chdir not working
by ikegami (Patriarch) on Mar 14, 2011 at 21:54 UTC

    btw, if $cwd contains the current work directory,

    my $path_data = $cwd."\\data"; chdir($path_data)
    is the same as
    my $path_data = "data"; chdir($path_data)

      Can I make it can-insensitive?for example am trying to append "data",if the directory contains "DATA",will windows be able to find it?

        The Windows OS file system is case-preserving but case-insensitive.

        c:\>cd @wOrK\PeRl c:\@Work\Perl> c:\@Work\Perl>dir uPD* Volume in drive C is Acer Volume Serial Number is 1234-5678 Directory of c:\@Work\Perl 02/13/2010 07:57 PM 4,098 Updfxgen.pl 02/13/2010 07:57 PM 3,956 Updfxgen.pl1 02/13/2010 07:57 PM 33,199 updgen.pl 02/13/2010 07:57 PM 1,732 Updungen.pl 02/13/2010 07:57 PM 1,589 Updungen.pl1 5 File(s) 44,574 bytes 0 Dir(s) 256,281,104,384 bytes free

        Update: Expanded the example. Also: Perl commands for the Windows API (not for the command line!) accept a / (forward slash) just as well as a \ (backslash), alleviating the need to escape backslashes in double-quoted strings.

        If the file system is case-insensitive, then chdir('data'), chdir('Data'), chdir('DATA'), etc will all switch to your data directory, however it was named at creation.

        If the file system is case-sensitive, then only one of chdir('data'), chdir('Data'), chdir('DATA'), etc will switch to your data directory.

      This is not working for me,am using 5.6.1,does that make a difference?

      On a side note,how do I update my previous replies

        You can't update your previous replies. You are Anonymous Monk which has the small advantage of not needing to register or log on, but has many disadvantages too like not being able to edit your nodes or being advised when someone has replied to one of your nodes.

        True laziness is hard work
        You need to have been logged in when you made the post. Otherwise, anyone would be able to update any anonymous post.

      This is not working for me,am using 5.6.1,does that make a difference?

        What do you mean by not working? I didn't say either would work; I said they were the same. Do you mean they're not the same for you? If so, why do you think they are different?

      This is not working for me,am using 5.6.1,does that make a difference?