unix code to windows/max?

2 views (last 30 days)
Aerice
Aerice on 31 May 2011
[EDIT: 20110530 19:33 CDT - reformat - WDR]
I'm trying to convert some code so it will work on a windows or mac. For instance,
[c,num] = unix('find /home/Blah/blah_files/* -type f | wc -l');
directory_string = '/home/Blah/blah_files/';
filename = strcat('/home/Blah/blah_files/',d(count+2).name);
num_of_records = hdfread(filename, '/blah_metadata', 'Fields', 'NUMBEROFRECORDS', 'FirstRecord',1 ,'NumRecords',1);
Thanks.

Answers (1)

Walter Roberson
Walter Roberson on 31 May 2011
dsname = '/blah_metadata';
if ispc
directory_string = 'C:/Documents and Settings/Blah/blah_files';
elseif ismac
directory_string = '/Users/Blah/blah_files';
else
directory_string = '/home/Blah/blah_files';
end
d = dir(directory_string);
d([d.isdir]) = []; %remove directories
c = length(d);
for count=1:c
filename = fullfile(directory_string, d(count).name);
num_of_records = hdfread(filename, dsname, 'Fields', 'NUMBEROFRECORDS', 'FirstRecord',1 ,'NumRecords',1);
fprintf('%s had %d records\n', d.name, num_of_records);
end
Some notes here:
  • You can use either / or \ as the directory separator for Windows, but in the long run you will probably encounter fewer problems if you use / rather than \ . Windows itself is defined to accept either.
  • Your existing code counts all of the files under the base directory, including in subdirectories (unless the subdirectory name begins with a period), but the code you showed for constructing the filename is not suitable for looking in subdirectories . As you started with unix code and so presumably already have the code working to your satisfaction for that case, I recoded to assume that subdirectories are not taken in to account.
  • I remove the '.' and '..' and all other subdirectories from d, rather assuming that '.' and '..' are the first two entries and skipping the first two entries. You should not assume that dir() returns file names in any kind of sorted order -- not even on Windows. The '.' entry could be (for example) the 19th instead of the first.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!