Access UNC Windows path using variable as server name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roblenator
    New Member
    • Jun 2007
    • 3

    Access UNC Windows path using variable as server name

    I am trying to access files on a remote windows server using the following code;
    [CODE=perl]
    my $node = 'server5';
    opendir(DIR, '\\$node\C$\Ser vers') or warn "open failed. reason: $!";
    [/CODE]

    I get an error; open failed. reason: no such file or directory at ... line xx.

    If I place the node name in place of the $node, it works fine.
    [CODE=perl]
    opendir(DIR, '\\server5\C$\S ervers') or warn "open failed. reason: $!";
    [/CODE]

    And yes, $node = 'server5';

    I am running this from Windows Server 2003 and all of the servers I am trying to connect to are windows servers as well.

    I have also tried "\\\\$node\\C$\ \Servers" and '//$node/C$/Servers' and "//$node/C$/Servers" all with the same error as above.

    Please respond to <REMOVED BY MODERATOR>

    Thanks,

    Rob
    Last edited by miller; Jun 5 '07, 07:39 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings Rob,

    You're using a single quoted string which does not allow interpolation. Whenever working with directories or files, it often helps to assign the directory or file name to a variable before using it in an opendir or open statement. This is useful since it allows you to also include the same file or directory name in your error checking statement, which would have clued you into the problems.

    [CODE=perl]
    my $node = 'server5';
    my $dir = "\\\\$node\\C\$ Servers";
    opendir(DIR, $dir) or die "Can't open $dir: $!";
    [/CODE]

    Now, I don't know what path you are trying to open, so I can't really advise you on what the syntax of $dir should be. But this methodology should at least let you attempt to get it working right. Good luck.

    - Miller

    Comment

    • roblenator
      New Member
      • Jun 2007
      • 3

      #3
      Thank you thank you thank you!!!

      The problem was my C$ needed to be C\$.

      So, the syntax opendir(DIR, "\\\\$node\\C\$ \\Servers") is the correct syntax.

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Glad I could help.

        - Miller

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by roblenator
          Thank you thank you thank you!!!

          The problem was my C$ needed to be C\$.

          So, the syntax opendir(DIR, "\\\\$node\\C\$ \\Servers") is the correct syntax.

          That is the correct syntax, but \$ was only part of the problem. As Miller pointed out, you were using single-quotes to construct the string, when you needed to use double-quotes.

          Comment

          Working...