Renaming multiple files in a folder

I have files within a folder that have a common filename that I need to omit from all of the files.
For Example:
Antenna_REF_Angle00.s2p
Antenna_REF_Angle45.s2p
Antenna_REF_Angle90.s2p
I want to removed the word "REF" from all of the files in a single loop. Can someone help? Thanks

 Risposta accettata

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.
% CHANGE THESE FOLDER NAMES!!!!!!
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);
uiwait(warndlg(errorMessage));
return;
end
% 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.
end
% 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);
end
uiwait(msgbox('Done copying files!', 'modal'));

6 Commenti

Hi, Thank you for the response.
Instead of replacing the "_REF" with null how can I replace part of the filename to "REFL"?
I wanted to have the ability to rename different files with different names.
Thanks!!
The third argument is the replacement string so if you want _REF to be "REFL" you'd do
baseFileName2 = strrep(baseFileName, '_REF', 'REFL');
Thank you
Michael Angeles
Michael Angeles il 19 Gen 2023
Modificato: Michael Angeles il 19 Gen 2023
I tried to run the code, where does it exactly move the new file to?
for example, I removed the _REF on line 26
if ~contains(baseFileName, 'WRXX_THRU', 'IgnoreCase', true)
also changed line30
baseFileName2 = strrep(baseFileName, 'WRxx_THRU', 'WRxx_D86958_2_PR_THRU');
I don't see any changes. Sorry, I am still learning Matlab
Image Analyst
Image Analyst il 19 Gen 2023
Modificato: Image Analyst il 19 Gen 2023
It doesn't move it. I know, it's a bad name, but "movefile" is actually the name of the function to rename a file, as well as to move it to a different folder if you want. But if the folder prefixing the filename is the same for both source and destination folder, then the operation will all be in that folder and it's essentially a renaming operation. It doesn't move it anywhere unless the folder prefixed to the front of the full file name is different between the two.
If baseFileName2 was no different than baseFileName, then 'WRxx_THRU' did not occur in baseFileName. If the search string is not there, it just returns the string unaltered.
Awesome. Thanks again for the help...There is so much to learn with Matlab

Accedi per commentare.

Più risposte (2)

Jan
Jan il 16 Gen 2023
Modificato: Jan il 16 Gen 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);
end
MFK
MFK il 16 Gen 2023
dinfo=dir('*.s2p');
for id = 1:length(dinfo)
% Get the file name
[~, f,~] = fileparts(dinfo(id).name);
newname = regexprep(f,'_REF_','');
movefile(dinfo(id).name,[newname '.s2p']);
end

1 Commento

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).

Accedi per commentare.

Categorie

Scopri di più su Communications Toolbox in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by