spm_check_filename.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. function V = spm_check_filename(V)
  2. % Checks paths are valid and tries to restore path names
  3. % FORMAT V = spm_check_filename(V)
  4. %
  5. % V - struct array of file handles
  6. %__________________________________________________________________________
  7. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  8. % Karl Friston
  9. % $Id: spm_check_filename.m 3934 2010-06-17 14:58:25Z guillaume $
  10. if isdeployed, return; end
  11. % check filenames
  12. %--------------------------------------------------------------------------
  13. for i = 1:length(V)
  14. % see if file exists
  15. %----------------------------------------------------------------------
  16. if ~spm_existfile(V(i).fname)
  17. % try current directory
  18. %------------------------------------------------------------------
  19. [p,n,e] = fileparts(V(i).fname);
  20. fname = which([n,e]);
  21. if ~isempty(fname)
  22. V(i).fname = fname;
  23. else
  24. % try parent directory
  25. %--------------------------------------------------------------
  26. cwd = pwd;
  27. cd('..')
  28. fname = which([n,e]);
  29. if ~isempty(fname)
  30. V(i).fname = fname;
  31. else
  32. % try children of parent
  33. %----------------------------------------------------------
  34. V = spm_which_filename(V);
  35. cd(cwd)
  36. return
  37. end
  38. cd(cwd)
  39. end
  40. end
  41. end
  42. %==========================================================================
  43. function V = spm_which_filename(V)
  44. % get children directories of parent
  45. %--------------------------------------------------------------------------
  46. cwd = pwd;
  47. cd('..')
  48. gwd = genpath(pwd);
  49. addpath(gwd);
  50. % cycle through handles
  51. %--------------------------------------------------------------------------
  52. for i = 1:length(V)
  53. try
  54. % get relative path (directory and filename) and find in children
  55. %------------------------------------------------------------------
  56. j = strfind(V(i).fname,filesep);
  57. fname = which(fname(j(end - 1):end));
  58. if ~isempty(fname)
  59. V(i).fname = fname;
  60. end
  61. end
  62. end
  63. % reset paths
  64. %--------------------------------------------------------------------------
  65. rmpath(gwd);
  66. cd(cwd);