469,306 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Access UNC Windows path using variable as server name

I am trying to access files on a remote windows server using the following code;
Expand|Select|Wrap|Line Numbers
  1. my $node = 'server5';
  2. opendir(DIR, '\\$node\C$\Servers') or warn "open failed. reason: $!";
  3.  
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.
Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, '\\server5\C$\Servers') or warn "open failed. reason: $!";
  2.  
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
Jun 5 '07 #1
4 7594
miller
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. my $node = 'server5';
  2. my $dir = "\\\\$node\\C\$Servers";
  3. opendir(DIR, $dir) or die "Can't open $dir: $!";
  4.  
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
Jun 5 '07 #2
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.
Jun 5 '07 #3
miller
1,089 Expert 1GB
Glad I could help.

- Miller
Jun 5 '07 #4
KevinADC
4,059 Expert 2GB
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.
Jun 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Peter Frost | last post: by
1 post views Thread by Jody Gelowitz | last post: by
1 post views Thread by gm | last post: by
24 posts views Thread by dancer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.