batch importing files - double backslashes?

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

batch importing files - double backslashes?

Djoere Gaublomme
Hello,

I have run into a problem which might be quite trivial.

I am trying to batch process a folder, for which I used the 'Process  
Folder' template macro code (I have copy pasted it at the bottom of  
this message).
The first command is to import the file with Bioformats Importer,  
however I get an error:  ")" expected  .

The command is:
run("Bio-Formats Importer", "open=["input + file"] autoscale  
color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");

Where input is the path from getDirectory, and file is the filename.
I assume I am making a basic mistake here somewhere.

I thought the problem might be due to a case of single or double  
backslashes, as when you ' print(input + file) ', in the log there are  
only single backslashes visible in the path name (but perhaps  
internally the string does contain 2 backslashes?)



I looked at some suggestions online and tried replacing the input  
string as follows:

inputReal = replace(input,"\\" , "\\\\")
This gave the error:  //java.util.regex.PatternSyntaxException:  
Unexpected internal error
near index 1

or

inputReal = replace(input,"\\" , "/")
This gave the error: ' ";" expected '  , when trying to ' print(inputReal); '


Any suggestions?

Thanks

Djoere





'Process Folder' template macro code :

input = getDirectory("Input directory");
output = getDirectory("Output directory");

Dialog.create("File type");
Dialog.addString("File suffix: ", ".lsm", 5);
Dialog.show();
suffix = Dialog.getString();

processFolder(input);

function processFolder(input) {
        list = getFileList(input);
        for (i = 0; i < list.length; i++) {
                if(endsWith(list[i], "/"))
                        processFolder("" + input + list[i]);
                if(endsWith(list[i], suffix))
                        processFile(input, output, list[i]);
        }
}

function processFile(input, output, file) {
        // do the processing here by replacing
        // the following two lines by your own code
        print("Processing: " + input + file);
        print("Saving to: " + output);
        print(output);

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: batch importing files - double backslashes?

gankaku
Hi Djoere,

The error occurs because you are missing the connecting "+"-signs:

your code:
run("Bio-Formats Importer", "open=["input + file"] autoscale
color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");

corrected code:
run("Bio-Formats Importer", "open=[" + input + file + "] autoscale
color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");

This should solve the problem.

If you want to have the file and folder separators it might be better to
use the following command

variable1 + file.separator + variable2

The command "file.separator" gives you the correct separators no matter in
which system you are running the macro. Otherwise, it will be running only
on Windows or only on Mac since the file separators used in IJ are
different in the two systems.

cheers,
Jan

2015-02-10 10:53 GMT+01:00 Djoere Gaublomme <[hidden email]>:

> Hello,
>
> I have run into a problem which might be quite trivial.
>
> I am trying to batch process a folder, for which I used the 'Process
> Folder' template macro code (I have copy pasted it at the bottom of this
> message).
> The first command is to import the file with Bioformats Importer, however
> I get an error:  ")" expected  .
>
> The command is:
> run("Bio-Formats Importer", "open=["input + file"] autoscale
> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>
> Where input is the path from getDirectory, and file is the filename.
> I assume I am making a basic mistake here somewhere.
>
> I thought the problem might be due to a case of single or double
> backslashes, as when you ' print(input + file) ', in the log there are only
> single backslashes visible in the path name (but perhaps internally the
> string does contain 2 backslashes?)
>
>
>
> I looked at some suggestions online and tried replacing the input string
> as follows:
>
> inputReal = replace(input,"\\" , "\\\\")
> This gave the error:  //java.util.regex.PatternSyntaxException:
> Unexpected internal error
> near index 1
>
> or
>
> inputReal = replace(input,"\\" , "/")
> This gave the error: ' ";" expected '  , when trying to '
> print(inputReal); '
>
>
> Any suggestions?
>
> Thanks
>
> Djoere
>
>
>
>
>
> 'Process Folder' template macro code :
>
> input = getDirectory("Input directory");
> output = getDirectory("Output directory");
>
> Dialog.create("File type");
> Dialog.addString("File suffix: ", ".lsm", 5);
> Dialog.show();
> suffix = Dialog.getString();
>
> processFolder(input);
>
> function processFolder(input) {
>         list = getFileList(input);
>         for (i = 0; i < list.length; i++) {
>                 if(endsWith(list[i], "/"))
>                         processFolder("" + input + list[i]);
>                 if(endsWith(list[i], suffix))
>                         processFile(input, output, list[i]);
>         }
> }
>
> function processFile(input, output, file) {
>         // do the processing here by replacing
>         // the following two lines by your own code
>         print("Processing: " + input + file);
>         print("Saving to: " + output);
>         print(output);
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>



--

CEO: Dr. rer. nat. Jan Brocher
phone:  +49 (0)6234 917 03 39
mobile: +49 (0)176 705 746 81
e-mail: [hidden email]
info: [hidden email]
inquiries: [hidden email]
web: www.biovoxxel.de

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: batch importing files - double backslashes?

Djoere Gaublomme
OK, that was it.

Thanks a lot, Jan !

Djoere

Quoting BioVoxxel <[hidden email]>:

> Hi Djoere,
>
> The error occurs because you are missing the connecting "+"-signs:
>
> your code:
> run("Bio-Formats Importer", "open=["input + file"] autoscale
> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>
> corrected code:
> run("Bio-Formats Importer", "open=[" + input + file + "] autoscale
> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>
> This should solve the problem.
>
> If you want to have the file and folder separators it might be better to
> use the following command
>
> variable1 + file.separator + variable2
>
> The command "file.separator" gives you the correct separators no matter in
> which system you are running the macro. Otherwise, it will be running only
> on Windows or only on Mac since the file separators used in IJ are
> different in the two systems.
>
> cheers,
> Jan
>
> 2015-02-10 10:53 GMT+01:00 Djoere Gaublomme <[hidden email]>:
>
>> Hello,
>>
>> I have run into a problem which might be quite trivial.
>>
>> I am trying to batch process a folder, for which I used the 'Process
>> Folder' template macro code (I have copy pasted it at the bottom of this
>> message).
>> The first command is to import the file with Bioformats Importer, however
>> I get an error:  ")" expected  .
>>
>> The command is:
>> run("Bio-Formats Importer", "open=["input + file"] autoscale
>> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>>
>> Where input is the path from getDirectory, and file is the filename.
>> I assume I am making a basic mistake here somewhere.
>>
>> I thought the problem might be due to a case of single or double
>> backslashes, as when you ' print(input + file) ', in the log there are only
>> single backslashes visible in the path name (but perhaps internally the
>> string does contain 2 backslashes?)
>>
>>
>>
>> I looked at some suggestions online and tried replacing the input string
>> as follows:
>>
>> inputReal = replace(input,"\\" , "\\\\")
>> This gave the error:  //java.util.regex.PatternSyntaxException:
>> Unexpected internal error
>> near index 1
>>
>> or
>>
>> inputReal = replace(input,"\\" , "/")
>> This gave the error: ' ";" expected '  , when trying to '
>> print(inputReal); '
>>
>>
>> Any suggestions?
>>
>> Thanks
>>
>> Djoere
>>
>>
>>
>>
>>
>> 'Process Folder' template macro code :
>>
>> input = getDirectory("Input directory");
>> output = getDirectory("Output directory");
>>
>> Dialog.create("File type");
>> Dialog.addString("File suffix: ", ".lsm", 5);
>> Dialog.show();
>> suffix = Dialog.getString();
>>
>> processFolder(input);
>>
>> function processFolder(input) {
>>         list = getFileList(input);
>>         for (i = 0; i < list.length; i++) {
>>                 if(endsWith(list[i], "/"))
>>                         processFolder("" + input + list[i]);
>>                 if(endsWith(list[i], suffix))
>>                         processFile(input, output, list[i]);
>>         }
>> }
>>
>> function processFile(input, output, file) {
>>         // do the processing here by replacing
>>         // the following two lines by your own code
>>         print("Processing: " + input + file);
>>         print("Saving to: " + output);
>>         print(output);
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
>
>
> --
>
> CEO: Dr. rer. nat. Jan Brocher
> phone:  +49 (0)6234 917 03 39
> mobile: +49 (0)176 705 746 81
> e-mail: [hidden email]
> info: [hidden email]
> inquiries: [hidden email]
> web: www.biovoxxel.de
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: batch importing files - double backslashes?

Niko Ehrenfeuchter
Hi all,

on a side note: with very few exceptions [1], you can safely use the
forward slash "/" as the directory separator in Windows as well - from
my experience you can even mix forward and backslash in a single path
specification.

So, IMHO, there is no need to use the clumsy 14-character
"file.separator" variable from the macro language.

Cheers
Niko

[1] https://msdn.microsoft.com/en-us/library/aa365247.aspx

On 10.02.2015 11:41, Djoere Gaublomme wrote:

> OK, that was it.
>
> Thanks a lot, Jan !
>
> Djoere
>
> Quoting BioVoxxel <[hidden email]>:
>
>> Hi Djoere,
>>
>> The error occurs because you are missing the connecting "+"-signs:
>>
>> your code:
>> run("Bio-Formats Importer", "open=["input + file"] autoscale
>> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>>
>> corrected code:
>> run("Bio-Formats Importer", "open=[" + input + file + "] autoscale
>> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>>
>> This should solve the problem.
>>
>> If you want to have the file and folder separators it might be better to
>> use the following command
>>
>> variable1 + file.separator + variable2
>>
>> The command "file.separator" gives you the correct separators no
>> matter in
>> which system you are running the macro. Otherwise, it will be running
>> only
>> on Windows or only on Mac since the file separators used in IJ are
>> different in the two systems.
>>
>> cheers,
>> Jan
>>
>> 2015-02-10 10:53 GMT+01:00 Djoere Gaublomme <[hidden email]>:
>>
>>> Hello,
>>>
>>> I have run into a problem which might be quite trivial.
>>>
>>> I am trying to batch process a folder, for which I used the 'Process
>>> Folder' template macro code (I have copy pasted it at the bottom of this
>>> message).
>>> The first command is to import the file with Bioformats Importer,
>>> however
>>> I get an error:  ")" expected  .
>>>
>>> The command is:
>>> run("Bio-Formats Importer", "open=["input + file"] autoscale
>>> color_mode=Default display_metadata view=Hyperstack stack_order=XYCZT");
>>>
>>> Where input is the path from getDirectory, and file is the filename.
>>> I assume I am making a basic mistake here somewhere.
>>>
>>> I thought the problem might be due to a case of single or double
>>> backslashes, as when you ' print(input + file) ', in the log there
>>> are only
>>> single backslashes visible in the path name (but perhaps internally the
>>> string does contain 2 backslashes?)
>>>
>>>
>>>
>>> I looked at some suggestions online and tried replacing the input string
>>> as follows:
>>>
>>> inputReal = replace(input,"\\" , "\\\\")
>>> This gave the error:  //java.util.regex.PatternSyntaxException:
>>> Unexpected internal error
>>> near index 1
>>>
>>> or
>>>
>>> inputReal = replace(input,"\\" , "/")
>>> This gave the error: ' ";" expected '  , when trying to '
>>> print(inputReal); '
>>>
>>>
>>> Any suggestions?
>>>
>>> Thanks
>>>
>>> Djoere
>>>
>>>
>>>
>>>
>>>
>>> 'Process Folder' template macro code :
>>>
>>> input = getDirectory("Input directory");
>>> output = getDirectory("Output directory");
>>>
>>> Dialog.create("File type");
>>> Dialog.addString("File suffix: ", ".lsm", 5);
>>> Dialog.show();
>>> suffix = Dialog.getString();
>>>
>>> processFolder(input);
>>>
>>> function processFolder(input) {
>>>         list = getFileList(input);
>>>         for (i = 0; i < list.length; i++) {
>>>                 if(endsWith(list[i], "/"))
>>>                         processFolder("" + input + list[i]);
>>>                 if(endsWith(list[i], suffix))
>>>                         processFile(input, output, list[i]);
>>>         }
>>> }
>>>
>>> function processFile(input, output, file) {
>>>         // do the processing here by replacing
>>>         // the following two lines by your own code
>>>         print("Processing: " + input + file);
>>>         print("Saving to: " + output);
>>>         print(output);
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>>
>>
>>
>> --
>>
>> CEO: Dr. rer. nat. Jan Brocher
>> phone:  +49 (0)6234 917 03 39
>> mobile: +49 (0)176 705 746 81
>> e-mail: [hidden email]
>> info: [hidden email]
>> inquiries: [hidden email]
>> web: www.biovoxxel.de
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
Niko Ehrenfeuchter | Image Analysis Specialist | Biozentrum, University
of Basel | Klingelbergstr. 50/70 | CH-4056 Basel
Phone: +41 (61) 26 72673 | [hidden email] |
www.biozentrum.unibas.ch | www.microscopynetwork.unibas.ch

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: batch importing files - double backslashes?

Olivier Burri
> So, IMHO, there is no need to use the clumsy 14-character "file.separator"
> variable from the macro language.

While I agree with Nico, the nicest way of doing this is to declare a variable at the beginning of your macro

var s = File.separator;

And then use that.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html