Filename seperators - slash or backslash! 
Author Message
 Filename seperators - slash or backslash!

Anyone know how to join a directory name to a filename using the correct
separator, slash or backslash, in a portable manner?  I've used File::Find
to traverse a directory tree but it always uses a slash in File::Find::name
which results in "c:\fred/eric.pl" on NT which is an invalid filename and
cannot be opened.

I need a portable method as my code is required to work on various platforms
so no "win32" solutions please.

Thanks,
Paul DS.




Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!


Quote:
> Anyone know how to join a directory name to a filename using the correct
> separator, slash or backslash, in a portable manner?  I've used File::Find
> to traverse a directory tree but it always uses a slash in File::Find::name
> which results in "c:\fred/eric.pl" on NT which is an invalid filename and
> cannot be opened.

Use File::Spec to put together your file paths.

perldoc File::Spec



Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!

Quote:

> Anyone know how to join a directory name to a filename using the correct
> separator, slash or backslash, in a portable manner?  I've used File::Find
> to traverse a directory tree but it always uses a slash in File::Find::name
> which results in "c:\fred/eric.pl" on NT which is an invalid filename and
> cannot be opened.

Slash is always correct and should work on win
- Alex


Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!


Quote:

> > Anyone know how to join a directory name to a filename using the correct
> > separator, slash or backslash, in a portable manner?  I've used File::Find
> > to traverse a directory tree but it always uses a slash in File::Find::name
> > which results in "c:\fred/eric.pl" on NT which is an invalid filename and
> > cannot be opened.

> Slash is always correct and should work on win

Slas is not 'always' correct; on Macs it is ':'.  However...

Paul may have run into a double-quoting problem on that example
filename, which would have a form-feed character instead of a backslash
and an 'f'.  Single-quoting would work.

Any combination of forward slashes and backslashes works fine on
Windows/DOS systems.  So sane people use forward slashes exclusively,
except when talking to one of the dain-bread default command
interpreters (via system() or exec() or qx()).

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!

Quote:

>Paul may have run into a double-quoting problem on that example
>filename, which would have a form-feed character instead of a backslash
>and an 'f'.  Single-quoting would work.

>Any combination of forward slashes and backslashes works fine on
>Windows/DOS systems.  So sane people use forward slashes exclusively,
>except when talking to one of the dain-bread default command
>interpreters (via system() or exec() or qx()).

Back in the good old days of MS/PC DOS version 2 Microsoft was also
involved in unix development with the xenix product, and the way unix
handled file path names was carried over to dos.  There vere only
one problem and that was COMMAND.COM which used the slash as a switch
character, and to remain compatible, another character was needed as
a separator in file path names.  The backslash looked similar enough,
so that could be used as a separator in addition to the normal slash.

This even went so far that DOS had an int21 call to change the switch
character to for example '-' so you could do:

   copy /abc/old /abc/new -v

in good old unix style.

Most program developers didn't know this, or ignored it, so quite a
few programs dealing with dos filenames can't handle forward slash as
a separator in file path names.

Not sure if NT continues to use either slash character as a file path
separator.

In Perl (or C) you can decide whether you prefer "/abc/def" to "\\abc\\def"
as file names, although '\abc\def' with single quotes would work as well.

Villy



Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!

Quote:
> >Any combination of forward slashes and backslashes works fine on
> >Windows/DOS systems.  So sane people use forward slashes exclusively,
> >except when talking to one of the dain-bread default command
> >interpreters (via system() or exec() or qx()).

*SNIP*

> This even went so far that DOS had an int21 call to change the switch
> character to for example '-' so you could do:

>    copy /abc/old /abc/new -v

> in good old unix style.

> Most program developers didn't know this, or ignored it, so quite a
> few programs dealing with dos filenames can't handle forward slash as
> a separator in file path names.

> Not sure if NT continues to use either slash character as a file path
> separator.

        NT acknowledges only the backslash as a path separator, and if there's
a way to change things, it's well-hidden.  I'm pretty sure that no such
thing exists.  I have solved the filepath problem by installing the
Cygwin tools and setting PERL5SHELL to the bash that comes with it.  The
bash autotranslates filenames from slash to backslash notation, and has
a functional mount command that allows you to mount, say C:\TEMP to
/tmp, and supports symlinks (not the same as Windows shortcuts).  I
haven't done extensive work with this yet, but the preliminary tests
have shown that full scripting should work fine.

/|/|ike /+yers



Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!

Quote:

>> >Any combination of forward slashes and backslashes works fine on
>> >Windows/DOS systems.  So sane people use forward slashes exclusively,
>> >except when talking to one of the dain-bread default command
>> >interpreters (via system() or exec() or qx()).

>*SNIP*

>> This even went so far that DOS had an int21 call to change the switch
>> character to for example '-' so you could do:

>>    copy /abc/old /abc/new -v

>> in good old unix style.

>> Most program developers didn't know this, or ignored it, so quite a
>> few programs dealing with dos filenames can't handle forward slash as
>> a separator in file path names.

>> Not sure if NT continues to use either slash character as a file path
>> separator.

>    NT acknowledges only the backslash as a path separator, and if there's
>a way to change things, it's well-hidden.  I'm pretty sure that no such
>thing exists.

True, no such -v switch exists for command.com or cmd.exe in NT, but it is still
true that NT accepts slash or backslash internally, such as from a C call or a
Perl function. In fact, even command.com used accept either. You just had to
have the -v option to get it to NOT parse the slash as an option and leave it
alone in the path.


Wed, 18 Jun 1902 08:00:00 GMT  
 Filename seperators - slash or backslash!

Quote:

>In Perl (or C) you can decide whether you prefer "/abc/def" to "\\abc\\def"
>as file names, although '\abc\def' with single quotes would work as well.

Except if you have two backslashes next to each other, e.g. in UNC
names. '\\server\share\filename.ext' won't work; you still have to use
'\\\\server\share\filename.ext' (or '\\\\server\\share\\filename.ext').

Cheers,
Philip
--



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. problem with slash and backslash : strange behaviour with different PC

2. Slash vs backslash in nt vs 95?

3. NT Perl 5 and slash vs. backslash

4. Substituting backslash for 2 forward slashes

5. Changing back slashes to forward slashes

6. Getting long filename from 8.3 filename?

7. Help in converting unix filenames to legit msdos filenames

8. Help in converting unix filenames to legit msdos filenames

9. extract filename from /path/path/filename

10. Newbie - A Regular Expression for Record Seperator $/

11. Multiple field seperators with split()

12. Input Record Seperator question

 

 
Powered by phpBB® Forum Software