Renaming multiple files in a folder

2 views (last 30 days)
I have files within a folder that have a common filename that I need to omit from all of the files.
For Example:
I want to removed the word "REF" from all of the files in a single loop. Can someone help? Thanks

Accepted Answer

Image Analyst
Image Analyst on 16 Jan 2023
Try this (untested):
% Removes _REF from all filenames in a folder.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
% Define input and output folders.
topLevelFolder = pwd;
outputFolder = topLevelFolder;
% Check to see that folder exists.
if ~isfolder(topLevelFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', topLevelFolder);
% Get a list of files to copy.
filePattern = fullfile(topLevelFolder, '*.*'); % All files in topLevelFolder ONLY.
% filePattern = fullfile(topLevelFolder, '**/*.*'); % All files in topLevelFolder and subfolders of it.
% filePattern = fullfile(inputFolder, '*.m'); % m-files.
fileNamesToTransfer = dir(filePattern);
numFiles = length(fileNamesToTransfer);
% Do the copying.
for k = 1 : numFiles
% Get the base file name.
baseFileName = fileNamesToTransfer(k).name;
% Skip file if it does not contain "_ref".
if ~contains(baseFileName, '_REF', 'IgnoreCase', true)
continue; % Skip this file.
% Replace '_REF' with null.
baseFileName2 = strrep(baseFileName, '_REF', '');
baseFileName2 = strrep(baseFileName2, '_ref', ''); % Do it for lower case also.
inputFolder = fileNamesToTransfer(k).folder;
% Create the full input and output filenames.
fullInputFileName = fullfile(inputFolder, baseFileName);
fullOutputFileName = fullfile(outputFolder, baseFileName2);
fprintf(1, 'Now renaming file #%d of %d: %s to %s\n', ...
k, numFiles, fullInputFileName, fullOutputFileName);
movefile(fullInputFileName, fullOutputFileName);
uiwait(msgbox('Done copying files!', 'modal'));
Michael Angeles
Michael Angeles on 19 Jan 2023
Awesome. Thanks again for the help...There is so much to learn with Matlab

Sign in to comment.

More Answers (2)

Jan on 16 Jan 2023
Edited: Jan on 16 Jan 2023
Folder = 'C:\Your\Folder';
FileList = dir(fullfile(Folder), 'Antenna_REF_*.s2p');
for k = 1:numel(FileList)
oldName = FileList(k).name;
newName = strrep(oldName, 'REF', '');
[status, msg] = movefile(fullfile(folder, oldName), fullfile(folder, newName));
assert(status == 1, msg);

MFK on 16 Jan 2023
for id = 1:length(dinfo)
% Get the file name
[~, f,~] = fileparts(dinfo(id).name);
newname = regexprep(f,'_REF_','');
movefile(dinfo(id).name,[newname '.s2p']);
  1 Comment
Image Analyst
Image Analyst on 16 Jan 2023
To use this answer, since the code does not use fullfile(), you need to make sure that the current folder is the data folder. So the folder where the files are also must contain the above script (m-file).

Sign in to comment.


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!