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

I want to print all the files present in the folder calc in C drive of the server named ax-risvis447

I am using the following code :

$DIR = '\\ax-risvis447\c$\calc'; print `dir $DIR`;

However, the above code gives the error :

D:\pmacs\Test_Nayab>perl adhoc_file_transfer.pl
The system cannot find the file specified.
\\AX-RISCVMCRYS02\C$\CALC

Please help me monks. How to access particular drive on a windows server through perl.

Replies are listed 'Best First'.
Re: Access a particular drive on windows server
by Anonymous Monk on Apr 24, 2014 at 12:52 UTC

    In your string, "\\" is an escape sequence that turns into "\":

    my $DIR; $DIR = '\\ax-risvis447\c$\calc'; print $DIR; # prints "\ax-risvis447\c$\calc" # This gets you the string you want: $DIR = '\\\\ax-risvis447\c$\calc'; print $DIR; # prints "\\ax-risvis447\c$\calc" # And if you want to use double quotes, escape the "$" too: $DIR = "\\\\ax-risvis447\\c\$\\calc"; print $DIR; # prints "\\ax-risvis447\c$\calc"

    However, you may still have issues due to shell escapes sometimes being tricky on Windows. For a "cleaner" way to list the files in a directory, see this recent thread for several approaches.

    If you still have problems - what happens when you type in dir \\ax-risvis447\c$\calc on the command line?

    As a general side note, you should be using use warnings; use strict; at the top of your scripts, to help avoid some common mistakes and typos.

      Thanks for the reply monk. Sorry, I am using the server : AX-RISCVMCRYS02

      When i ran the command dir \\AX-RISCVMCRYS02\C$\CALC on the command prompt.
      I am getting the same error

      Same error as the perl file:

      The system cannot find the file specified.
      \\AX-RISCVMCRYS02\C$\CALC

        Then the error you're seeing in Perl is actually coming from your OS, and you need to solve that problem first. Since we don't know anything about your network setup, your network administrator will likely be of much more help. (Just an idea, you may need to supply login credentials and/or mount the network share via something like net use.)

Re: Access a particular drive on windows server
by VincentK (Beadle) on Apr 25, 2014 at 15:13 UTC
    Good day nayabrahil

    As far as I know, the DIR command does not support UNC file paths. You will most likely need to map that path to a drive letter on your machine.

    Alternatively I located a library that seems to work by mapping the UNC path to a temporary location.

    use strict; use warnings; use File::pushd; my $TEMP_DIR_OF_UNC = pushd( '\\AX-RISCVMCRYS02\C$\CALC' ); print "Temporary mapping of UNC path is : $TEMP_DIR_OF_UNC\n"; system('DIR '.$TEMP_DIR_OF_UNC);

    I suggest reading over the manual page for the Pushd library. There are some usage gotcha's in there.

    http://search.cpan.org/~dagolden/File-pushd-1.006/lib/File/pushd.pm

    I hope this helps.

      Awesome monks you people are doing a wonderful job. God knows what will happen to perl developers without you people.

      I will try the above method and will let you know if it works for me. Thanks again.