Posted by
Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Problems-with-File-exists-and-s-tp3692201p3692203.html
Hi Adam,
double backslashes are required when specifying a String constant,
i.e., if you put a string under quotes somewhere into your program.
This is necessary because the backslash character is otherwise
interpreted as a modifier to the character following it.
Example:
"\n" is a string with one character, the newline character.
"\\n" is a string with two characters, the backslash character and
the 'n' (lowercase letter).
Printing the first will create a new line; printing the second will
print a backslash folowed by an 'n'.
This is a matter of representation in the program text only.
File.exists() does not need double slashes in the string, so the
string that you get from File.directory is fine. If you want to
append something that you specify as a string with quotes, you need :
mySubdirectory5 = File.directory + "samples5\\";
myFile = mySubdirectory5 + "test.tif";
if (File.exists(myFile))
print("yes, it exists");
If File.directory is C:\Documents and Settings\Joe\samples\
then mySubdirectory5 will be C:\Documents and Settings\Joe\samples
\samples5\
and myFile will be C:\Documents and Settings\Joe\samples\samples5
\test.tif
So, if you really want to replace single backslashes by double ones,
you need
replace(file_check, "\\","\\\\")
but this is not needed in your case.
One more remark: you can also use slashes instead of backslashes, in
file names. You need not duplicate slashes in string constants.
Still confused?
Michael
________________________________________________________________
On 10 Jun 2009, at 07:32, Adam Hacking wrote:
> Hi
>
> File.directory seems to return path with 1 slash \
>
> When checking is a file exists using the above,
>
> File.exists() seems to require that the path contain (2) forward
> slashes \\ ie
>
> C:\\imageJ\\directory\\some_file.txt
>
> Without such it retuns false when it should return true.
>
> replace(file_check, '\','\\'); is problematic b/c it seems to be an
> escape character.
>
> Is there a simple solution here ?
>
> Thanks
>
> Adam
>
>
>
>
>
>
>
>