Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

System substitution

by bitman (Scribe)
on Nov 20, 2002 at 11:47 UTC ( #214402=perlquestion: print w/replies, xml ) Need Help??
bitman has asked for the wisdom of the Perl Monks concerning the following question:

I need to run a dos program from Perl. Now the program string and the parameter have imbedded spaces, I am having problems with my escaping. I have tried all sorts of variations. Currently I have the executable working, but the parameter fails. This works :-
$ENV{TYPE}='c:\scotts test\type me'; $rc = system("\"$ENV{TYPE}\" c:\\test.bat");
This fails :-
$ENV{TYPE}='c:\scotts test\type me'; $new_debug_log='c:\scotts test\type me.bat'; $rc = system("\"$ENV{TYPE}\" \"$new_debug_log\"");
type me.bat contains
@echo off @type %1
It's driving me - crazy

Replies are listed 'Best First'.
Re: System substitution
by robartes (Priest) on Nov 20, 2002 at 12:36 UTC
    You need to make sure that the shell sees the "scotts test" and "type me" as one name each, and not parse the space in between as a seperator. Conversely, the space before your argument should be parsed as a separator:
    use strict; my $command='c:\"scotts test"\"type me"; my $argument='c:\"this is a"\"multi word argument"'; print $command." ".$argument; __END__ c:\"scotts test"\"type me" c:\"this is a"\"multi word argument"
    If you replace the print by a call to system(), that should do the trick. It's been a couple of years since I've done anything on the Windows command line, so I might be wrong in my quoting syntax, which would kind of defeat the entire purpose of this post, but there you go :)


      on win2k, atleast, these are equivalent:

      C:\>"c:\my test\type me" "c:\my test\type me.bat" @echo off @type %1 C:\>c:\"my test"\"type me" c:\"my test"\"type me.bat" @echo off @type %1
      Any idea about older win32s?

      Rock is dead. Long live paper and scissors!
Re: System substitution
by LTjake (Prior) on Nov 20, 2002 at 12:34 UTC
    When i run your program, i get the following output (on win2k):
    $ENV{TYPE}='c:\my test\type me'; $new_debug_log='c:\my test\type me.bat'; $rc = system("\"$ENV{TYPE}\" \"$new_debug_log\""); __output__ @echo off @type %1
    That seems right to me, type prints out the contents of the file, and that happens to be the contents of that file. I would probably use the q() command instead of escaping things.
    $ENV{TYPE} = q("c:\my test\type me"); $new_debug_log = q("c:\my test\type me.bat"); $rc = system("$ENV{TYPE} $new_debug_log"); __output__ @echo off @type %1

    Rock is dead. Long live paper and scissors!
Re: System substitution
by AcidHawk (Vicar) on Nov 20, 2002 at 13:04 UTC

    I find problems with this all the time.. I have had to resort to the following ...

    use win32; $path = "C:\\Test Path\\test file.bat"; $parameter = $ARGV[0]; unless ($bat_file = Win32::GetShortPathName($path)) { print "$bat_file - Path Problems in Setup for $path: $^E\n"); exit(); } $bat_file =~s /\\/\//g; print "$bat_file\n"; system("$bat_file $parameter");
    I have tested this code and it does take the command line param from the .pl script and place it after the shortened path with the bat file.

    I was only echoing a hello %1 in the bat file and from the command line running AcidHawk which resulted in the bat file echoing Hello AcidHawk..

    Update: Hmm... let me relook at this question..

    You can use the Win32::GetshortPathName for both your $ENV{TYPE} as well as your $new_debug_log. The code above can still apply though instead of using $ARGV[0] use your $new_debug_log var that you have got the short path name for..

    Of all the things I've lost in my life, its my mind I miss the most.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://214402]
Approved by robartes
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-02-22 07:46 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.