change filename from different folder
Mostra commenti meno recenti
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 Commenti
Marcelo Costa
il 20 Nov 2012
for return the name of file use c(1).name
Virginie
il 22 Nov 2012
MatlabPro
il 22 Nov 2012
why not try loop/
Virginie
il 22 Nov 2012
Modificato: Walter Roberson
il 22 Nov 2012
Risposte (2)
John Petersen
il 20 Nov 2012
0 voti
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 Commenti
Walter Roberson
il 21 Nov 2012
Modificato: Walter Roberson
il 21 Nov 2012
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
il 22 Nov 2012
Walter Roberson
il 22 Nov 2012
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
il 22 Nov 2012
Image Analyst
il 22 Nov 2012
Walter Roberson
il 22 Nov 2012
As I wrote, "and then you will need to check whether c(i).name matches '.' or '..' to eliminate those two."
Virginie
il 22 Nov 2012
Walter Roberson
il 22 Nov 2012
if strcmp(c(i).name, '.') || strcmp(c(i).name, '..'); continue; end
Virginie
il 22 Nov 2012
Virginie
il 22 Nov 2012
Walter Roberson
il 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.
Image Analyst
il 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.
1 Commento
Virginie
il 22 Nov 2012
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!