0

I have a script that looks at a given directory and grabs all the files for processing. The issue is that I'd like it to ignore any hidden (.) files. I saw a number of ways to do this in other posts like this and this, but my case is so simple, I thought it was worth asking as I'm new to octave. Worth mentioning that the 2nd link is simple, but seems strange that I would have to use a for loop to filter...

Here's my code:

dirname = strcat(pwd, '/fileset');
files = dir(dirname);
fileidx = find(~[files.isdir]);

for i = 1:length(fileidx)
    filename = [dirname, '/', files(fileidx(i)).name];
    fid = fopen(filename, 'r');
end
Community
  • 1
  • 1
jml
  • 1,745
  • 6
  • 29
  • 55

1 Answers1

2

I had promised "some more love" for that first line... "the one that didn't work". Here it is.

There are some interesting ways to manipulate cell arrays that I didn't know until I researched this answer a bit more... In particular, the use of cellfun(). I am going to share what I learned.

For example, when you have a cell array of filenames

fnames = {files.name};

you can apply a regex to find files that match a certain criterion (e.g. "doesn't start with a dot")

crit = '^[^.]+';

% return cell array of results, [] if no match
rxResult = regexp( fnames, crit ); 

% loop over all cells, set true if regex matched
nodot = (cellfun('isempty', rxResult)==0); 

% The `nodot` array is a logical array telling us which are "good" files. 
totalGoodFiles = sum(nodot);

Now you can use

fileidx = find(~[files.isdir] & nodot);

On the other hand, you are using a for loop already, so you could put your test in there (edited to include file count):

fCount=0;
for i = 1:length(fileidx)
if( files(fileidx(i)).name(1) ~= '.')
      filename = [dirname, '/', files(fileidx(i)).name];
      fCount = fCount+ 1;
      fid = fopen(filename, 'r');
    end
end

As an aside, there is the function fullfile() which you can use to concatenate directories and file names; it understands the correct file separator to use for your operating system. Quite handy - better than concatenating '/'. When you try to run on a Mac it will fail...

A further aside - the built in constant filesep will return the correct file path separator to use on your platform; good for making your code portable.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • I tried it in the for loop and it seems to work. The first portion of your answer might need some love, though. – jml Feb 07 '13 at 05:29
  • The concatenation works on Mac. I'm using octave though, so IDK if that matters. – jml Feb 07 '13 at 05:30
  • Glad you are out of your bind... surprised that the Mac isn't barfing at the '/' (although that's the file separator it uses at the kernel level, it likes ":" at the higher level. Who knows what it's thinking...) I will figure out the "love" for the first line when I'm back at work. – Floris Feb 07 '13 at 05:33
  • Yeah, I think I need the first bit actually. I'm counting the files as I go along and any hidden files being counted in that for loop are going to be misleading to users. – jml Feb 07 '13 at 06:55