http://www.perlmonks.org?node_id=480751

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

Dear Monks,

I have little bit trouble in system function. If i use the file name which has space in it, it creates problem.

example:

system ("adrl.exe d:\anniyan\filemod\chapter1.doc");

it works well, whereas the following is not

system ("adrl.exe d:\anniyan\file mod\chapter 1.doc");

What is the reason and solution for such problems?

Regards,
Anniyan
(CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Replies are listed 'Best First'.
Re: space in filepath and in filename for system function
by gellyfish (Monsignor) on Aug 04, 2005 at 10:12 UTC

    You probably want to use the multiple argument system :

    system ('adrl.exe','d:\anniyan\file mod\chapter 1.doc');
    With the single argument your file name will be broken up on white space and passed to the program as multiple arguments.

    /J\

      re. With the single argument your file name will be broken up on white space and passed to the program as multiple arguments.

      I expected this to be the case, but before posting a similar reply to yours I went and tested it with two tests (because the OP is using DOS -- I speak unix, which would not accept this and I normally only speak DOS under duress, usually in the form of extra money ;)):

      cd I AM AN EXISTING DIR

      type I AM AN EXISTING FILE

      The DOS session (under windows NT) managed to perform these correctly and as single arguments rather than trying to access a list of files.

      One world, one people

      Thanks for your reply.

      I have stored that path in a variable $file.

      It is passing the variable name itself as a argument.

      system ('adrl.exe', '$file') || die ("not correct path");

      Regards,
      Anniyan
      (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

        Well yes. It would. If you put it in a single-quoted string. Variables aren't expanded in single-quoted strings. You need a double-quoted string. Or, actually, you don't need any quotes at all.

        system ('adrl.exe', $file) || die ("not correct path");
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: space in filepath and in filename for system function
by swift (Sexton) on Aug 04, 2005 at 11:19 UTC

    You can set file name and its path into a variable and use quotemeta()

    $file=quotemeta('d:\anniyan\file mod\chapter 1.doc'); system ("adrl.exe" $file);
Re: space in filepath and in filename for system function
by furry_marmot (Pilgrim) on Aug 04, 2005 at 15:43 UTC
    DOS/Windows needs double quotes, not single quotes, to quote things. And when you use the name of a file with a space in it, you need to quote the name, since the command-line interpreter breaks things up by space. Thus, on the command line, you could type

    adrl.exe "d:\anniyan\file mod\chapter 1.doc"

    and that will work fine. So, for your perl script, consider (among other solutions)

    system qq(adrl.exe "d:\anniyan\file mod\chapter 1.doc");