OneK
OneK

Reputation: 705

open UNC path with spaces in windows explorer with perl

Hello stackoverflowers,

i'm afraid i can't figure out how to open an UNC path with spaces within Windows Explorer in perl. Purpose is, i want the user to put a file within the path. To make it more comfortable, the path should open in explorer automatically. Which it does for any local drives.

The UNC path that should open is: \\srvr1\mean space dir

My code so far:

use strict
use warnings

my $sourceDir = "\\\\srvr1\\mean space dir";
system("start $sourceDir");

Which gives the error: "Win can't access \\srvr1\mean." Ok, so i tried to quote the string:

my $sourceDir = "\\\\srvr1\\\"mean space dir\"";

Which lead to: "Win can't access \\srvr1\"mean space dir"."

Next thing i tried was:

my $sourceDir = q{"\\\srvr1\\mean space dir"}

Which lead to an cmd window being opened with the correct path within the title?!

Is maybe the system call itself wrong? I really appreciate any help. Thanks.

Upvotes: 2

Views: 811

Answers (2)

Nick P
Nick P

Reputation: 769

For this kind of thing the array style system call is a good fit. You don't need to worry about quoting the path or escaping as much.

$path = '\\\\srvr1\mean space dir';
system('start', '', $path);

Quoting (or forgetting to quote) paths in system calls is a significant source of bugs where I've worked. A habit of doing it as above means you never need to worry about it.

Upvotes: 2

nobody
nobody

Reputation: 20174

The second form is correct, but then you have to account for the fact that the start command treats its first quoted argument as a window title. If you want to start a quoted path, you need to give a window title argument too (the empty string is fine).

Like so:

my $sourceDir = q{\\\\srvr1\\mean space dir};
system(qq{start "" "$sourceDir"});

Upvotes: 4

Related Questions