[Perl] run a program in windows

Nimster nimster at netvision.net.il
Tue Feb 19 06:56:23 PST 2002

----- Original Message -----
From: "Sibony, Rony" <Rony.Sibony at comverse.com>
To: <perl at perl.org.il>
Sent: Tuesday, February 19, 2002 3:51 PM
Subject: RE: [Perl] run a program in windows

> I tried this -
> system ("C:\\Program Files\\Windows Media Player\\wmplayer.exe"
> "\c:\\my_music_file.mp3"");
> but this one dose'nt working it came out with the error
> 'c:\Program' is not recognized as an internal or external command,
> operable program or batch file.

The answer: system("\"C:\\Program\ Files\\Windows\ Media\
Player\\wmplayer.exe\"\ C:\\my_file.mp3");

Why?: It's windows' cmd.exe that's giving your problems with the spaces, and
it's windows that needs to be taken care of with surrounding double-quotes.
Perl passes it on just as it should. If you type "C:\Program Files\..." in
the command prompt it will give you the same error.

Another method: You can use Windows shorthands for names over 8.3: trunctate
any spaces, and add ~x where x is the number of the instance resulting with
the same name, for example:
(Robd-c~1.mp3, for example, is Rob D - Clubbed to Death.mp3. Windows Media
Player is Window~2 because in my case, I have 2 directories which would be
abbriviated to Window~1: Windows Media Player and Windows NT.)

Interesting bit: from perldoc -f system: "If there is only one scalar
argument, the argument is checked for shell metacharacters, and if there are
any, the entire argument is passed to the system's command shell for parsing
(this is "/bin/sh -c" on UNIX platforms, but varies on other platforms
[cmd.exe on windows - Nimster]). If there are no shell metacharacters in the
argument, it is split into words and passed directly to "execvp", which is
more efficient).
This means we can actually have it work just the same without the
escape-slash between the command ("C:\Program files\...") and the argument
("myfile.mp3"). For consistancy, I'd keep it. Besides, it means less work
for the Perl interperter in splitting the words. In fact, if we do regard
overhead as much as we should, we can already use exec() ourselves instead
of having system() check for meta characters.
One final word of warning - as a UNIX developer you should know that if you
give your users the option to type the name of the file, one malevolent user
might try typing in "& deltree C:\". I believe that if you indeed use exec()
instead, it will not be passed to cmd.exe for evaluation of metacharacters,
and instead wmplayer.exe should return some error on a missing file. I have
not tried this (yet).

> Rony Sibony
> +972-51-232031
> +972-3-6452031

More information about the Perl mailing list