필터 지우기
필터 지우기

change filename from different folder

조회 수: 1 (최근 30일)
Virginie
Virginie 2012년 11월 20일
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
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일
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
Virginie
Virginie 2012년 11월 22일
It's working now. Thanks a lot!
Walter Roberson
Walter Roberson 2012년 11월 22일
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일
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.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by