How to remove first few characters from multiple file names?

I have multiple files with similar first few characters stored in a folder. How would I remove the first few letters from multiple files so that I am only left with characters that follow the first few letters?
For example: Original - "Mark_Mathworks_3088" change to "Mathworks_3088"? In the example I want to remove "Mark_", I have few files with "Mark" in the front and others with different names such as - "Nat", "Watt", & "Rick" which I want to remove as well.
What would be a correct code to make this work?

Answers (1)

Get the string first from your structure or cell array.
filename = dirListing.name; % if you got by using dir() function or
filename = cellArray{index}; % if it's in a cell array.
Then find the underline and get from there on:
underlineLocation = strfind(filename, '_');
filename = filename(underlineLocation + 1 : end);

5 Comments

I am not familiar with Matlab, Can you please show the full explanation of the code and how to write it.
That is how you write it. Those are actual lines of MATLAB code. If you don't know the first thing about MATLAB, take the 2 hour training course here: https://matlabacademy.mathworks.com/
You'd do something like (untested):
inputFolder = pwd; % Wherever you want.
outputFolder = pwd; % Wherever you want. Can be the same as inputFolder if you want.
dirListing = dir(fullfile(inputFolder, '*.m'));
for k = 1 : length(dirListing)
baseFileName = dirListing(k).name;
underlineLocation = strfind(baseFileName, '_');
if ~isempty(underlineLocation)
outputBaseFileName = baseFileName(underlineLocation + 1 : end);
inputFileName = fullfile(inputFolder, baseFileName);
outputFileName = fullfile(outputFolder, outputBaseFileName);
fprintf('Ranaming %s to %s.\n', baseFileName, outputFileName);
if isequal(inputFolder, outputFolder)
% Rename in place.
movefile(inputFileName, outputFileName);
else
% Make copy in other folder.
copyfile(inputFileName, outputFileName);
end
end
end
The files are not in .m format. They are in folder with different file types. Some are .docx, .pdf, and some in .text format.
So, simply change the .m to .docx, .pdf, .*, or whatever in the file pattern in the call to dir(). It doesn't matter to the rest of code.
I tried using this code to shorten the name of my files. I just wanted to keep the first 21 letters of the file name and save the new files in a new folder.
It did all of that correctly, except when I ran the program, the files it saved in the new folder werent tifs anymore. How do I rename my files without making sure the content of the files remain unchanged?
clc
clear variables
close all
inputFolder = ('D:\Hytrel Testing\Hytrel EL217-5-2'); % Wherever you want.
outputFolder = ('D:\Hytrel Testing\Hytrel EL217-5-2\Renamed_Files'); % Wherever you want. Can be the same as inputFolder if you want.
dirListing = dir(fullfile(inputFolder, '*.tif'));
for k = 1 : length(dirListing)
baseFileName = dirListing(k).name;
%underlineLocation = strfind(baseFileName, '_');
if ~isempty(baseFileName)
outputBaseFileName = baseFileName(1 : 21);
inputFileName = fullfile(inputFolder, baseFileName);
outputFileName = fullfile(outputFolder, outputBaseFileName);
fprintf('Ranaming %s to %s.\n', baseFileName, outputFileName);
if isequal(inputFolder, outputFolder)
% Rename in place.
movefile(inputFileName, outputFileName);
else
% Make copy in other folder.
copyfile(inputFileName, outputFileName);
end
end
end

Sign in to comment.

Categories

Asked:

on 11 Jan 2018

Commented:

on 25 Jan 2019

Community Treasure Hunt

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

Start Hunting!