Clear Filters
Clear Filters

change filename from different folder

1 view (last 30 days)
Virginie
Virginie on 20 Nov 2012
Hi everybody,
I've lots of files, in different folder, which I'd liked to change the name. So I wanted to get the name of the folders using 'dir' :
>> c=dir
c =
125x1 struct array with fields:
name
date
bytes
isdir
datenum
>> c(1)
ans =
name: '.'
date: '14-Nov-2012 15:27:02'
bytes: 0
isdir: 1
datenum: 7.3519e+05
So as you see, it doesn't return me any name. Normally, it should be '364D' (first folder)>
If I get a variable with my folder name, I'd like to do something like that:
foldername=dir
for i=1:length(foldername);
cd foldername(i)
filename=dir *.sac.inv;
for i=1:length(filename);
loadsac(waveform,filename(1))
cmp=get(h,'component');
station=get(h,'station');
time=get(h,'time');
name=cmp_station_foldername(i)_time.sac.inv;
save(name)
end
cd ..
end
Because my origin file form is 'a3114001.sac.inv' and I'd like 'EW_BACA_343A_031306.sac.inv' with the information contains in the header of the file. Do you think it's possible?
Thank you in advance,
Virginie
  4 Comments
Virginie
Virginie on 22 Nov 2012
Edited: Walter Roberson on 22 Nov 2012
I've tried:
for i=1:125
c=dir;
c=c(i).name;
c(i)=[c(i)]
end
It gives me name but c=c(i).name; doesn't keep all the name so I tried to create a matrix c(i)=[c(i)] to keep them. But I've some difficulties... Could you help me please.
Thanks in advance,
Virginie

Sign in to comment.

Answers (2)

John Petersen
John Petersen on 20 Nov 2012
The first filename in a directory is the "." file which is just specifying the path to this directory... not really a file at all. The second "file" in a DOS directory is ".." which specifies the path just one folder above the current one. All the rest of the files or folders within this folder are after this. The first real file or directory other than these two would be at c(3).name.
  11 Comments
Virginie
Virginie on 22 Nov 2012
It's working now. Thanks a lot!
Walter Roberson
Walter Roberson on 22 Nov 2012
Every folder has a subfolder named '.', which refers to the exact same directory. This allows filenames such as ./startup.m to be processed more easily; in this eaxample it means "stay in the current directory and look for startup.m here". If you were to give a command such as
edit startup.m
then normally MATLAB would search along the MATLAB path for a startup.m file along that path and open the first one it found, but if you
edit ./startup.m
then it would mean "only look for startup.m here in the current directory".
Each folder also contains a subfolder named '..' which refers to the parent folder. For example if you were in /User/people/Virginie/matlab then '..' relative to that folder would refer to /User/people/Virginie/ . This is very useful for creating and using bundles of related directories without needing to figure out where you started from.
Usually when you encounter those two directories, you just want to skip them. That is what the "continue" command is for in the code sample I showed: "continue" inside a "for" or "while" loop means to continue on with the next loop iteration, skipping over everything else until the matching "end" statement.

Sign in to comment.


Image Analyst
Image Analyst on 22 Nov 2012
If all your files have a .inv extension, why not simply say
folder = pwd; % not foldername=dir like you had!!!
filePattern = fullfile(folder, '*.inv');
filenames = dir(filePattern );
?????? Of course that could all be done in one line if you wanted to be less explicit. Then you wouldn't have to worry about dot, dot dot, etc.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!