By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Access UNC Windows path using variable as server name

P: 3
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
Share this Question
Share on Google+
4 Replies


miller
Expert 100+
P: 1,089
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

P: 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.
Jun 5 '07 #3

miller
Expert 100+
P: 1,089
Glad I could help.

- Miller
Jun 5 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
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.