Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Im trying to run an application out of perl, but the path contains files with blank characters. Like C:\program files... so perl is trying to execute c:\program. Is there any way around this? Thank guys, Chucky

Replies are listed 'Best First'.
Re: windows file name probs
by dree (Monsignor) on Jun 05, 2002 at 16:41 UTC
    Put " in the beginning and in the end of the path string, like:
    system ("\"C:\\Program Files\\myexe.exe\"");

Re: windows file name probs
by Rex(Wrecks) (Curate) on Jun 05, 2002 at 16:44 UTC
    I assume you are using backtick or system(). If so the Windows command processor needs double quotes around the strings with spaces, and don't forget to deref the '\'.

    Sample:
    #!/usr/bin/perl use strict ; my $test3 = `dir \"C:\\Program files\"` ; print ("Test: $test3\n") ;


    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: windows file name probs
by coreolyn (Parson) on Jun 05, 2002 at 17:47 UTC
    Rex(Wrecks)

    Just a slight variation as this may come in handy if you are inserting a directory name from a loop. You only need the quotes around the part of the directory that contains spaces.

    Stealing from Rex(Wrecks) example:

    #!/usr/bin/perl use strict ; my $drive = "C:"; my $cmd = "dir"; my $dir = "Program files"; if ($dir =~ /\s/ ) { $dir = "\"$dir\""; } my $test3 = "$cmd $drive\\$dir"; print ("Test: $test3\n") ; system($test3);

    coreolyn
Re: windows file name probs
by Steve_p (Priest) on Jun 05, 2002 at 21:57 UTC
    I read some of the other solutions. Of course, in Perl, there is always another way. If your using ActiveState, perl is nice enought to switch "/" to "\". The following code demonstrates it.
    use strict; my $file = "c:/My Documents/class.cpp"; open MYFILE, "<$file" or die "Can't open: $!"; while(<MYFILE>){ print $_; }
Re: windows file name probs
by t0mas (Priest) on Jun 06, 2002 at 05:59 UTC
    The Win32 module comes with an interface to the GetShortPathName function.
    GetShortPathName makes a DOS (8.3) path of any valid Win32 path and will remove all spaces.

    The following code will start notepad (or whatever you have associated with .ini) and load c:\Program Files\desktop.ini
    #!/usr/bin/perl -w use strict; use Win32; system(Win32::GetShortPathName('c:\Program Files\desktop.ini'));

    OK, silly example, but...

    /brother t0mas