Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32: "system" call when command has spaces 1

Status
Not open for further replies.

GDameron

Programmer
Aug 8, 2003
39
US
Running version 5.005_03 under Windows 2000.

I am attempting to launch wordpad.exe, giving its full path, using "system". The code (file "wp.pl") looks like this:

Code:
use strict;
my @systemCmd =
  ("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe", "");
system (@systemCmd);

I run it from a cmd.exe window like so:

<perl's path>\perl.exe -w wp.pl

Oddly, the wordpad window actually does come up, but with an error dialog titled "Microsoft Visual C++ Runtime Library", stating:

Runtime Error!

Program: C:\Program Files\Windows NT\Accessories\wordpad.exe

abormal program termination

I have two workarounds so far:

1. If I add wordpad.exe's location to $ENV{'PATH'}, and reduce @systemCmd to just ("wordpad.exe", ""), it works.
2. If I copy wordpad.exe to a folder whose path has no spaces, and use that path in @systemCmd instead, it works.

Things I have tried (that haven't helped):

A. Using forward slashes instead of double backslashes;
B. Using single quotes instead of double quotes;
C. Using \x20 instead of spaces;
D. Using %20 instead of spaces (?? beats me - saw it in thread219-740789 for a similar problem - wordpad wouldn't even spawn in this case).

I can live with one of the workarounds, I suppose, but I'd prefer to use the full path as-is. Suggestions?

Thanks,
GD
 
Following works for me (Win XP):
Code:
#!perl
use strict;
my $systemCmd = 
"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
my $result=system($systemCmd);
Note that $systemCmd is a scalar here instead of a list and I've dropped the empty "" argument.
Have you tried this?
 
Or:

my @systemCmd =
('C:\Program Files\Windows NT\Accessories\wordpad.exe', "");

The double quotes on their own forces interpolation, whereas single quotes takes as is and passes it to system. This way you don't have to worry about escaping the backslashes either.


Barbie
Leader of Birmingham Perl Mongers
 
Thanks to all for the suggestions.

Coderifous (jim): "exec" won't do in this case because my code has other things to do after wordpad is launched and exits. (But thanks.)

mikevh: good suggestion, but I get the infamous 'C:\Program' is not recognized... error. Doesn't like the space. Could be a 2000 vs. XP thing.

missbarbell (Barbie): Yes, that was item B in my "haven't helped" list (original post) - I get the abnormal termination error. (But thanks.)

Combining mikevh's and missbarbell's suggestions (single quotes AND scalar) gives the same result as mikevh's suggestion alone.

Keep those ideas coming - I'm grateful for the help.

GD
 
GD,

why do you want to open wordpad at all in the middle of a script, you can write PDF's RTF's DOCs from Perl directly?

--Paul
 
Valid question. My users require the ability to manually edit an existing text file (generated earlier in the processing, and processed further after they leave the editor). I'm permitting them to bring up the file in either notepad or wordpad (their choice) to do the job.

GD
 
Too many choices ... too much hurt

Is it sorted yet?

--Paul
 
Have you tried passing $systemCmd (including the quotes) to the qq// function? This should get Windows to "see" the quotes and not quit at the first blank. I tried the following and it worked.
Code:
my $systemCmd = 
qq("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
my $result=system($systemCmd);
 
Good shot, mikevh. "qq" did the trick. I am able to bring up the file to be edited using:
Code:
my $systemCmd  = qq("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe" C:\\tmp\\myfile.txt);  # all one line

system($systemCmd);
(The filename argument can't have any quotes around it or the spawn won't work.)

And even better for my situation, I can say:
Code:
my $cmd = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
my $arg = "C:\\tmp\\myfile.txt";
my $systemCmd = qq("$cmd" $arg);
system($systemCmd);
Thanks for your help. You've earned your star.

GD
 
be warned if you want to use this on other Windows OS', you might want to figure out another way, the older versions of DOS don't like spaces in paths, they preffer the ~ symbol.

---------------------------------------
If you helped... thx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top