Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How do I open a file in a path that has spaces in the name?

( #175536=categorized question: print w/ replies, xml ) Need Help??
Contributed by maslas on Jun 18, 2002 at 23:39 UTC
Q&A  > directories


Description:

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?

Answer: How do I open a file in a path that has spaces in the name?
contributed by grinder

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?

Answer: How do I open a file in a path that has spaces in the name?
contributed by luzaranza

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

Answer: How do I open a file in a path that has spaces in the name?
contributed by ezechiel

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);

Answer: How do I open a file in a path that has spaces in the name?
contributed by ezechiel

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);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2014-12-20 12:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (95 votes), past polls