views:

1050

answers:

8

When defining a path to a directory as a variable or constant, should it end with a trailing slash? What is the convention?

pwd in unix shows your current directory without a trailing slash, while the tab complete of cd /var/www/apps/ includes the trailing slash, which left me unsure.

A: 

Whenever I store directory paths, or return them from APIs, I try and stick with the convention of keeping a trailing slash. This avoids the whole 'is it a file or a directory' ambiguity.

Addendum:
This is not intended to be a substitute for using methods that can tolerate either a trailing slash or its absence. Even using this convention I still always use Path.Combine(...) and similar methods.

jerryjvl
+1  A: 

I tend to just add the trailing slash as I am more than likely going to use that directory to add/retrieve files...

In terms of web referencing it can actually increase performance leaving the trailing slash in

http://www.netmechanic.com/news/vol4/load_no11.htm

James
+1  A: 

I've never seen a firm convention either way.

Pretty sure, though, that whatever you settle upon, someone else will be 100% sure it should be the other way. So, best idea is to tolerate things being set either way.

In the .NET world, Path.Combine() gives you a way to handle this - there are equivalents in other environments, from cmd files on up.

Bevan
+2  A: 

I don't include the trailing slash when I for example define a directory for storing files. That is because I will use it like

$store_file = "$store_path/$file_id";

I will always add a trailing slash before using a variable that's supposed to hold a directory path. I think it's better to always add one than to wonder if the trailing slash is included.

Johan Soderberg
A: 

I think there's no fixed convention.

I have my own bunch of utility routines for joining paths and filenames together, which insert the path separators as required when constructing paths.

String s1 = joinPaths("c:\root", "myfile.txt");  // = "c:\root\myfile.txt
String s2 = joinPaths("c:\root\", "myfile.txt"); // = "c:\root\myfile.txt
Roddy
don't forget your @ in there so you are not escaping the last "
Phill Duffy
Can't get it to work. Sorry...
Roddy
+3  A: 

Yes it should, as:

Pathname + filename = fully qualified file location.

SO the slash between the last directory and the filename needs to be either at the end of the pathname or the start of the filename. Prefixing filenames with a / means you need to take this into account if you just want to open a file (i.e if you assume that a unqualified filename is in the current working directory).

Visage
.. and failing to take it into account means you're possibly inadvertently messing with /, and that way madness lies
JustJeff
A: 

Yes, there are lots of filesystems that support files without any extensions, so always add the trailing slash to avoid any problems.

maxp
A: 

I go with the trailing slash because:

  1. "If it ends with a slash, it's a directory. If not, it's a file." is an easy convention to remember.

  2. At least on the OSes I commonly use, doubling the slash causes no problems, while omitting the slash causes big ones. It is, therefore, safest to both put the slash into the variable and use "$path/$file" when making use of it.

Dave Sherohman