Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Best way to work with Directory and File Paths independent of OS?

Guest
Apr 19, 2011 Apr 19, 2011

I'm working on a bit of code that manages files on the server. The problem is that my local instance is Macintosh and the server is Windows. I can dynamically find the location of the files throught the Server object but when I specify the rest of the directory I need to use the "/" for Macintosh and "\" for windows.

At this point my choice is to litter the code with If/Else statements to specify the correct path. I was wondering if there were any other options I was missing. The only other solution I had, which seemed to still be clunky and time consuming as it adds logic is to create a concatDirectory function. Where you pass it the directory, and the directory to concatenate it with. That function would determine and apply the correct slash.

3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 19, 2011 Apr 19, 2011

Put a single CFIF at the top of your code, or in your app.cfm/cfc file, that determines which OS it is running on, and defines a var that is either / or \.  Then use that varname in your code instead of / or \ - but be sure you are looking at the server scope to get the OS info, not the cgi vars, since those are from the client.

-reed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 19, 2011 Apr 19, 2011

Just use the universal forward slash and never worry about it again.

Backslash is NOT a path separator!

http://corfield.org/blog/post.cfm/Backslash_is_NOT_a_path_separator

OK, now I'm ticked! WTF is it with CFers that they think they have to mess about determining what the path separator is on every platform? In a pathetic attempt to be cross-platform, I see all sorts of convoluted code that tries to strip characters out of paths or check the O/S version in an attempt to establish whether \ or / is the right thing to use. Word up CFers: /

always works. We're using Java now and / works on Windows. Yes, really. / works on Windows, Mac and Unix. Instant cross-platform compatibility. Making a path work on all platforms is a matter of simply doing:

path = replace(path,"\","/","all);

No messing about. Just convert Windows silly \ to / and you're done. It works everywhere. Please stop trying to be cross-platform and do the right thing instead. What triggered this rant? The latest varScoper code doesn't work on Mac OS because of this nonsense. Change line 29 of varScoper.cfm to just:

<cfset var pathsep = "/" />

None of that silly checking platform stuff. "/". Just. Works.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 19, 2011 Apr 19, 2011

>>WTF is it with CFers that they think they have to mess about determining  what the path separator is on every platform?

>>Word up CFers: / always works

> Just use the universal forward slash and never worry about it again.

This is actually untrue, if taken (too) literally. If you use cfexecute tag and try to pass a path as an attribute to an executable, like <cfexecute.... arguments="-outfile c:/temp/file.tmp">, it won't work. You have to use backslashes as it IS the windows path separator. :-).

I know, the above is not really a direct coldfusion attribute value, so I'm not disagreeing with what was said. . Forward slash is always the way to go for all paths, application path variables, etc.

-Fernis

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 21, 2011 Apr 21, 2011

In your onApplicationStart():

<cfset application.PathSeparator = createObject("java","java.io.File").separator />

You can then reference #application.PathSeparator# in your code and it'll return either a forward or back slash.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 21, 2011 Apr 21, 2011
LATEST

Another consideration here is if one is ever outputting a file-system path on the UI (for whatever reason), it looks really amateurish to use the "wrong" path-separator when creating the path string, eg outputting something like C:\webroots\site/path/to/something/.  I see this not infrequently.

Given it's so easy to use the correct file separator, why not?

That said, for file operations internal to CF... just use a fwd slash.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources