Discussion:
file size in macro
Bruno Keijzers
2014-09-12 09:53:59 UTC
Permalink
Hi,

I wrote a macro that list files in a directory if the file size is less than some number.
This does not seem to work if the size is e.g. 10000 but does work for 9999 .. ?

dir = getDirectory("Choose a Directory ");
list = getFileList(dir);
print("Number of files: "+ list.length);
j=0;
for (i=0; i<list.length; i++) {
path = dir+list[i];
// if(File.length(path) < 10000) { //exclude long files ... does not work
if(File.length(path) < 9999) { //exclude long files ... does work
print(File.length(path) + " " + i + " " + list[i]);
j=j+1;
}
}
print("Number small files: "+ j);

What is wrong?

Bruno

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Rasband, Wayne (NIH/NIMH) [E]
2014-09-12 13:57:36 UTC
Permalink
Post by Bruno Keijzers
Hi,
I wrote a macro that list files in a directory if the file size is less than some number.
This does not seem to work if the size is e.g. 10000 but does work for 9999 .. ?
dir = getDirectory("Choose a Directory ");
list = getFileList(dir);
print("Number of files: "+ list.length);
j=0;
for (i=0; i<list.length; i++) {
path = dir+list[i];
// if(File.length(path) < 10000) { //exclude long files ... does not work
if(File.length(path) < 9999) { //exclude long files ... does work
print(File.length(path) + " " + i + " " + list[i]);
j=j+1;
}
}
print("Number small files: "+ j);
What is wrong?
The File.* macro functions return a string so the string returned by File.length() needs to be converted into a number before it can be used in a boolean expression. This conversion is done automatically when you use File.length() in an assignment statement, for example

length = File.length(path);
if (length < 9999) {

Or use the parseInt() function to convert the string into a number, for example

if (parseInt(File.length(path)) < 9999) {

-wayne
Post by Bruno Keijzers
Bruno
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Loading...