No BSD License  

Highlights from
FixFileName

image thumbnail
from FixFileName by Michael Robbins
removes invalid symbols from a filename taking the OS into account, automatically or by specifying a

newfilename=fixfilename(oldfilename,sysname,usespaces,eightthree,extendascii)
function newfilename=fixfilename(oldfilename,sysname,usespaces,eightthree,extendascii)
% FIXFILENAME(oldfilename,sysname,usespaces,eightthree)
% removes such invalid symbols from a filename
% taking the OS into account, automatically or by specifying as
% a parameter
%
% You should update the definitions in the switch statement; I did not
% take much care in researching them.
%
% INPUTS        DEFINITION        DEFAULT VALUE
%   OLDFILENAME input             {'adhj?*.8&a','AC li*e.cfg','Lo%g.txt'};
%   SYSNAME     specified OS      automatic
%   USESPACES   include spaces?   1
%   EIGHTTHREE  use 8.3 format    0
%   EXTENDASCII Use chars 128-255 1
%
% OUTPUTS
%   NEWFILENAME
%
% USAGE
%  disp('USE DEFAULT');
%  disp('fixfilename');
%  fixfilename
%
%  disp('SPECIFY IN AND OS');
%  disp('fixfilename({''adhj?*.8&a'',''AC li*e.cfg'',''Lo%g.txt''},''DOS''))');
%  fixfilename({'adhj?*.8&a','AC li*e.cfg','Lo%g.txt'},'DOS')
%
%  disp('SPECIFY IN, OS and USESPACES');
%  disp('fixfilename({''adhj?*.8&a'',''AC li*e.cfg'',''Lo%g.txt''},''WIN'',1)');
%  fixfilename({'adhj?*.8&a','AC li*e.cfg','Lo%g.txt'},'WIN',1)
%
%  disp('SPECIFY IN, OS, not USESPACES and EIGHTTHREE');
%  disp('fixfilename({''adhj?*.8&a'',''AC li*e.cfg'',''Lo%g.txt''},''WIN'',0,1)');
%  fixfilename({'adhj?*.8&a','AC li*e.cfg','Lo%g.txt'},'WIN',0,1)
%
%  disp('SPECIFY IN, not USESPACES and EIGHTTHREE WHILE LETTING THE FUNCTION');
%  disp('SPECIFY THE os');
%  disp('fixfilename({''adhj?*.8&a'',''AC li*e.cfg'',''Lo%g.txt''},[],0,1)');
%  fixfilename({'adhj?*.8&a','AC li*e.cfg','Lo%g.txt'},[],0,1)
%
% IT'S NOT FANCY, BUT IT WORKS

% Michael Robbins
% michael.robbins@us.cibc.com
% robbins@bloomberg.net

if nargin<1 | isempty(oldfilename)
   oldfilename={'adhj?*.8&a','AC li*e.cfg','Lo%g456789.txt'};
end;
if nargin<2 | isempty(sysname)
   [temp,sysname]=system('ver');
   sysname=sysname(2:end-1);
end;
if nargin<3 | isempty(usespaces)
    usespaces=1;
end;
if nargin<5 | isempty(eightthree)
    eightthree=0;
end;
if nargin<5 | isempty(extendascii)
    extendascii=1;
end;

switch upper(sysname)
case {'WIN','WINDOWS NT VERSION 4.0  '},
   notresvd='\d\w~!@#$%^&()_\-{}. ';
case 'DOS',
	% Letters A through Z. 
	% Digits 0 through 9. 
	% Characters with ASCII codes greater than 127.
    %    Note that these characters depend on current code table.
    %    Also, their handling is case sensitive. 
	% Space. Note that many applications do not recognize this character
    %    and it is not used in creating aliases for long filenames. 
	% $ % - _ @ ~ ` ! ( ) { } ^ # & 
   notresvd='\d\w$%''`\-@{}~!#()&_^.';
   eightthree=1;
case 'UNIX',
    notresvd='/';
otherwise
    error('Operating System name not recognized');
end;
notresvd=[notresvd '''+,;=[]`'];
if ~usespaces
    notresvd=strrep(notresvd,' ','');
end;
if extendascii
   notresvd=[notresvd char([128:255])];
end;
newfilename=regexprep(oldfilename, ...
  ['[^' notresvd ']*'],'');
if eightthree
    newfilename=regexprep(newfilename, ...
        '([^.]{1,8})[^.]*(\.[^.]{1,3})?', ...
        '$1$2','tokenize');
end;

Contact us