Renaming multilple .txt files
Show older comments
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 Comments
dpb
on 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.
Alex M.
on 31 Jul 2017
Accepted Answer
More Answers (1)
Vipresh Gangwal
on 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 ".."
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!