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

hi everybody iam using a command "sub{system "start soffice $file"})" to open a external word file but the problem is the file name has a space in it so it cant open the file please suggest me a way to open it .

20060106 Unconsidered by Corion: Keep: 11, Edit: 9, Reap: 0

  • Comment on how to open a file with space in its name

Replies are listed 'Best First'.
Re: how to open a file with space in its name
by Corion (Patriarch) on Jan 05, 2006 at 08:21 UTC

    Use the list version of system, which is safer anyway:

    my @cmd = ($ENV{COMSPEC}, "/c", "start", "soffice.exe", $file); system(@cmd) == 0 or die "Couldn't launch @cmd: $! / $?";
      Until very recently, perl on Win32 allegedly simply turned the system LIST into the system STRING version, without adding quotes. In other words, it still wouldn't have worked.

      I'm not sure when the backward incompatible change was introduced.

      it was working correctly thank you very much
Re: how to open a file with space in its name
by inman (Curate) on Jan 05, 2006 at 09:38 UTC
    I amd assuming that you are using Windows - A file path with embedded spaces needs to be delimited by double quotes. Add escaped double quotes around your $file. This is what you would have to do if you typed in the command yourself.

    sub{system "start soffice \"$file\""})

    As mentioned previously, the new style of system command removes this ambiguity and will make for a more portable program.

      If one is trying to escape filenames etc in a situation like this I prefer
      "start soffice \Q$file\E";
      to
      "start soffice \"$file\"";
      that way anything weird in the string is escaped, including " chars that may be in it... of course, the list version of system is safer.

                      - Ant
                      - Some of my best work - (1 2 3)

        Which will not work for Win32 filenames, as a file named c\:\\autoexec\.bat does not exist.

        Also, the quoting rule of double quotes (") is different for cmd.exe too - "" gets converted to ", """ gets converted to "" and so on. I think. In any case, quotemeta is not what you want when quoting strings for cmd.exe or command.com.

      In unix , how can we open a file with space in its name. Please paste the command and help me out.
        Use the multiple argument version of system
[OT] Re: how to open a file with space in its name
by astroboy (Chaplain) on Jan 05, 2006 at 14:23 UTC
    It looks like the OP may be performing this operation under Windows (space in the name). When specifically code for that OS, I usually pass the file name to Win32::GetShortPathName, which will provide the canonical short name (sans spaces, 8.3 format), and I dont have to worry about spaces in the names
Re: how to open a file with space in its name
by samizdat (Vicar) on Jan 05, 2006 at 14:01 UTC
    It also helps in making it universal to add './' (UN!X) or '.\' (M$W) to the front of the filename. Some web designers (including mine) use -filename.inc strings in order to make them difficult to read or list.

    Don Wilde
    "There's more than one level to any answer."