Problems with File.exists and \\'s

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems with File.exists and \\'s

Adam Hacking
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








Reply | Threaded
Open this post in threaded view
|

Re: Problems with File.exists and \\'s

Michael Schmid
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
>
>
>
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Problems with File.exists and \\'s

Michael Schmid
In reply to this post by Adam Hacking
Hi Adam,

yes, the result is correct.

In the String Constant
   "C:\Results.xls"
the sequence '\R' is translated into one character. As the sequence  
'\R' has no special meaning, it is simply translated into the letter  
'R'; and the resulting string has no backslash character in it.
   "C:\\Results.xls"
translates into a string that has one backslash.

---

waitForUser and threshold:
In a plugin you could add a MouseListener to the components of the  
Threshold window, but this would result in awful code. Also, it would  
be a problem if the user clicks on an arrow to shift the threshold  
slider - you never know what is the final click.

Michael
________________________________________________________________

On 10 Jun 2009, at 14:49, Adam Hacking wrote:

> Thanks.
>
> If I run
>
> file_check = replace("C:\Results.xls", "\\", "/");
>
> I get
>
> C:Results.xls
>
> Is this correct ?
>
>
> Regarding the threshold.....Is there any way to simply wait for  
> input from the threshold window ? I don't really need the message  
> generated by the wait.
>
> Thanks
>
> Adam
>
>
>
> --- On Wed, 6/10/09, Michael Schmid  wrote:
>
> 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
> >
> >
> >
> >
> >
> >
> >
> >
>