Renaming multilple .txt files
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello all,
I have been trying to rename multiple text files with different methods I found around but could not make it work. My files are name as such:
"Z_X12_ABC_000cm_1.txt" ;
"Z_X12_ABC_000cm_2.txt" ;
"Z_X12_ABC_000cm_3.txt" ;
Then,
"Z_X12_ABC_005cm_1.txt"
...
So I have 13 depths and a triplicate for each depth. Which gives me 39 files. In the folder, thanks to my labeling, they are all correctly ordered in alphabetical order.
I would simply would like to rename those 39 files as:
"Z_X12_ABC_1.txt" ;
"Z_X12_ABC_2.txt" ;
"Z_X12_ABC_4.txt" ;
"Z_X12_ABC_5.txt" ;
...
Any help would be appreciated.
2 Kommentare
dpb
am 28 Jul. 2017
If you do that as shown you'll destroy the sort order as
n =
'Z_X12_ABC_1.txt'
'Z_X12_ABC_2.txt'
'Z_X12_ABC_4.txt'
'Z_X12_ABC_5.txt'
'Z_X12_ABC_11.txt'
>> sort(n)
ans =
'Z_X12_ABC_1.txt'
'Z_X12_ABC_11.txt'
'Z_X12_ABC_2.txt'
'Z_X12_ABC_4.txt'
'Z_X12_ABC_5.txt'
>>
so you'll not have a 1:1 consonance of the name with the content. Why break a good scheme--what can you accomplish with the other naming scheme can't do as is? I could see eliminating the 'cm' and perhaps shortening the 3-digit field to two for a little brevity, but don't understand why would want to do what you've asked for, exactly; seems like disadvantages would outweigh any possible advantage.
Akzeptierte Antwort
dpb
am 31 Jul. 2017
Bearbeitet: dpb
am 31 Jul. 2017
OK. To respond to the question raised...
Well, that's the hard way...use
d=dir('Z_X12_ABC*.txt'); % return all files matching wildcard
for i=1:length(d) % iterate over the list
data=importdata(d(i).name); % read file
% process that file here
....
% and move on to next.
...
end
A major advantage of the above is there is no "magic number" of 39 buried in the code and you can modify the search pattern to select particular subsets of interest depending on the needs at the time.
If you really do need multiple files at one time, then you can use cell array or other storage mechanism, but often it isn't needed.
Or, of course, while it's somewhat more difficult to write, once you do, there's no issue in generating the names dynamically--
fmt='Z_X12_ABC_%03dcm_1.txt'; % filename pattern
for i=1:numFiles
data=importdata(sprintf(fmt,i)); % open file
...
Same comments apply about whether need more than one at a time but has the "magic number" issue plus is much more to make other selections with.
Here's example of the above...
>> for i=1:3,disp(sprintf(fmt,i)),end
Z_X12_ABC_001cm_1.txt
Z_X12_ABC_002cm_1.txt
Z_X12_ABC_003cm_1.txt
>>
And, the extension to incorporate the last cyclic numeric field should be obvious in a nested loop structure if you're adamant about doing it that way instead of dir
2 Kommentare
dpb
am 31 Jul. 2017
No problem, glad it helped. dir is always my first choice for such machinations unless there's some very specific reason not to use it.
Even if the wildcard matching needed is more than the OS pattern will do, note that you can use regexp on the result and do as sophisticated pattern matching as needed.
Weitere Antworten (1)
Vipresh Gangwal
am 28 Jul. 2017
try using the dir function with an output argument.
myFiles = dir
% sort myFiles structure if you need to
for i = 1:length(myFiles)
% current file = myFile(i);
% rename current file
end
Make sure to ignore the folders "." and ".."
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!