MATLAB Answers

Renaming multilple .txt files

10 views (last 30 days)
Alex M.
Alex M. on 28 Jul 2017
Commented: dpb on 31 Jul 2017
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
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.
Alex M. on 31 Jul 2017
The reason I do that is because I open all my text files using this:
numfiles = 39;
mydata = cell(1, numfiles);
for k = 1:numfiles
myfilename = sprintf('Z_X12_ABC_%d.txt', k);
mydata{k} = importdata(myfilename);
end
I haven't found a way to have a nested for loops going through the "000cm", then "005cm", etc.

Sign in to comment.

Accepted Answer

dpb
dpb on 31 Jul 2017
Edited: dpb on 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 Comments

Alex M.
Alex M. on 31 Jul 2017
The first approach using the dir function worked well for me. It saved me precious time, thank you!
dpb
dpb on 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.

Sign in to comment.

More Answers (1)

Vipresh Gangwal
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 ".."

  1 Comment

Alex M.
Alex M. on 31 Jul 2017
Vipresh, I'm not sure I understand what's inside the for loop works. I tried running it as it is in the folder where the .txt files are and it doesn't work. What should be modified in your code to adapt it to my case?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by