change filename from different folder

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

Marcelo Costa
Marcelo Costa 2012년 11월 20일
for return the name of file use c(1).name
Virginie
Virginie 2012년 11월 22일
It does't work... I still get the same thing.
MatlabPro
MatlabPro 2012년 11월 22일
why not try loop/
Virginie
Virginie 2012년 11월 22일
편집: Walter Roberson 2012년 11월 22일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

John Petersen
John Petersen 2012년 11월 20일

0 개 추천

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

Walter Roberson
Walter Roberson 2012년 11월 21일
편집: Walter Roberson 2012년 11월 21일
Correction: some names that dir() outputs will be directory names. This includes the names '.' and '..' on all operating systems that MATLAB currently runs on (I can't swear it was the case back when VMS was supported.) '.' and '..' are not necessarily the first two entries, though. The order defined for dir() is "whatever the operating system returns". The order defined for Unix-based operating systems is "whatever the file system returns". There have been file systems that did not return '.' and '..' first: in particular in some Unix filesystems, it is possible to create directories that will sort before '..' in the filesystem.
To eliminate directories, do not expect them to be at any particular location: instead use
c([c.isdir]) = []
to remove the entries.
Virginie
Virginie 2012년 11월 22일
I'm not sure to have well understood.'dir' gives you the path and not the name? Because I'd liked to get the string name of the folder in order to enter in every of them automatically using "cd foldername" and use the files that they are containing. I re-read the help of 'dir' and I think I'm using it wrongly. 'dir' gives the files inside the folder than I just want to get the folder names. I tryed 'ls', which list it but after I don't know how to keep this list in a variable. Do you know which command I could use.
Thank you very much for your help and your patience!
Virginie
Apply the dir() at the directory that contains the directories you want to process.
If you want to keep only the folders, then
c(~[c.isdir]) = [];
and then you will need to check whether c(i).name matches '.' or '..' to eliminate those two.
The names '.' and '..' are considered to be subdirectories of every directory.
When you
cd(c(i).name)
remember that you need to cd back again before you can do the next cd.
Virginie
Virginie 2012년 11월 22일
Sorry but it doesn't work... When I do "cd(c(1).name)", nothing happend. I think, it's because "c(1).name" = '.'. So when I do 'cd .' it just stayed where is it. I don't understand how it works! Thank you, Virignie
Image Analyst
Image Analyst 2012년 11월 22일
You're best off not using cd anyway. Use fullfile() instead. See the FAQ.
Walter Roberson
Walter Roberson 2012년 11월 22일
As I wrote, "and then you will need to check whether c(i).name matches '.' or '..' to eliminate those two."
Virginie
Virginie 2012년 11월 22일
Sorry, but I don't understand what you mean by "matches". With what?
if strcmp(c(i).name, '.') || strcmp(c(i).name, '..'); continue; end
Virginie
Virginie 2012년 11월 22일
Yes, they are matching. What does it mean?
Virginie
Virginie 2012년 11월 22일
It's working now. Thanks a lot!
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.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 11월 22일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2012년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by