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

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

I am trying to find certain files, and some of them have spaces in the path name; e.g.
c:\Program Files\My Projects\Scripts For Oracle\
The program finds the files, but the subsequent attempt to open the file fails:
open FILE, "> $filename" or die "Can't write $filename - $!";
What do I need to do so my script can open the file in a path like this?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I open a file in a path that has spaces in the name?
by Mr. Muskrat (Canon) on Jun 19, 2002 at 01:37 UTC
    The spaces are not the problem.
    Okay, first:
    use strict; use warnings;
    Second:
    If you are going to use double quotes, then you will need to use double backslashes or slashes. If you change the double quotes to single quotes all will be well. Example:
    #!/usr/bin/perl # ^ that should work for most Win32 installs use strict; use warnings; my $filename = "c:\\Program Files\\My Projects\\Scripts For Oracle\\te +st1.txt"; test_file($filename); $filename = "c:/Program Files/My Projects/Scripts For Oracle/test2.txt +"; test_file($filename); $filename = 'c:\Program Files\My Projects\Scripts For Oracle\test3.txt +'; test_file($filename); sub test_file { my $filename = shift; open(FILE, "> $filename") || die "Could not open the file $filename +for writing at "; print FILE "testing\n"; close(FILE); }
    I hope that I have made this clear enough.

    Originally posted as a Categorized Answer.

Re: How do I open a file in a path that has spaces in the name?
by grinder (Bishop) on Jun 19, 2002 at 10:43 UTC

    It may be that the problem lies not so much with the script itself, but the way you are calling it.

    If you have a file named filename with spaces then you cannot pass it to a script with

    script filename with spaces
    because the shell is going to pass that as three separate arguments, and so your script will look for one file named filename, a second file named with, and then a third file named spaces — any of which may or may not exist, but in any case won't be the file you meant to specify, the single file named filename with spaces.

    To pass the file to a script, it must therefore be quoted. The quoting rules depend on the environment you are using, but in general if you put double-quotes around the filename (whether or not it has spaces) you should be fine. On UNIXy systems, single-quotes work, as does "escaping" the spaces with backslashes:

    "filename with spaces" # works more or less universally 'filename with spaces' # works a lot of places, but not in the Windows + shell. filename\ with\ spaces # ditto
    And there may be other ways as well; check your system's documentation.

    See also this Q&A entry: How do you open a filehandle to a DOS file which has spaces in the filename?

Re: How do I open a file in a path that has spaces in the name?
by luzaranza (Initiate) on Feb 27, 2010 at 11:17 UTC
    This is a windows issue, when windows sees a path with spaces it reverts to 8.3 naming. This means that for a sub-directory such as Program Files gets renamed to: Progra~1 that is, it takes the first 6 characters of the pattern, appends the tilde, then a number representing the instance of that pattern. For example, in place of C:/Program Files/Jdk1.6.0_07/bin Use: C:/Progra~1/Jdk1.6.0_07/bin See this link for further details and full explanation of 8.3 naming http://support.microsoft.com/kb/142982
      Which version of Windows?

      I have no problem using files with spaces in the file or directory names in my Windows XP:

      use strict; use warnings; open my $fh, '<', 'c:\Directory with too many spaces\filename contains + spaces too.txt' or die "Cannot open file"; print while (<$fh>);

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How do I open a file in a path that has spaces in the name?
by ezechiel (Initiate) on Nov 03, 2009 at 10:57 UTC
    Sorry for second post, but forgot to format >_<

    Hi, I had the problem too but grinder gave me a hint:
    " It depends on the environment" and in Windows/DOS, directories with spaces are between quotes
    ex: c:\>dir "Program Files"

    So, in windows, escape space chars like this: (supposing you have a "test" directory in c:\

    $dir = "c:/test/\"Test Directory\"";
    mkdir ($dir);
      1. Perlmonks FAQ
      2. grinder did not say
        $dir = "\"Test Directory\""; mkdir ($dir) or die "$!\n$^E"; __END__ Invalid argument The filename, directory name, or volume label syntax is incorrect at - + line 2.
      3. grinder said quote your arguments, eg C:\> perl program.pl "i got spaces and I end up in $ARGV[0]" or
        C:\> perl -le"print for @ARGV" "i got spaces and I end up in $ARGV[0]" + me too i got spaces and I end up in $ARGV[0] me too
Re: How do I open a file in a path that has spaces in the name?
by ezechiel (Initiate) on Nov 03, 2009 at 10:51 UTC
    Hi, I had the problem too but grinder gave me a hint: " It depends on the environment" and in Windows/DOS, directories with spaces are between quotes ex: c:\>dir "Program Files" So, in windows, escape space chars like this: (supposing you have a "test" directory in c:\ $dir = "c:/test/\"Test Directory\""; mkdir ($dir);