Ergodicity 回复了一些很棒的代码,但是:
- 有些评论是德语而不是英语
- 普遍缺乏评论,这使得难以理解
我试图用下面的代码解决这些问题(我似乎无法使用“语言:lang-matlab”很好地以 MATLAB 格式显示代码,因此将其粘贴到 MATLAB 中以便于阅读):
close all;
clear all;
clc;
%% Parameters
% The directory in which to replace files. Currently this code does not modify files in
% sub-directories
directory = 'C:\Users\Name\Wonderful code folder';
% The string that will be replaced
oldString = sprintf('terrible mistake');
% The replacement string
newString = sprintf('all fixed now');
% The file name condition - what type of files will be examined
% It must contain any of the English character set (letters, numbers or underscore
% character i.e. a-zA-Z_0-9) and ends with a ".m" MATLAB extension (use \.txt for text files)
regularExpression = '[\w]+\.m';
%% Determine files to update, and update them as necessary
% Change the current directory to the user-specified one
cd(directory)
% Put the details of all files and folders in that current directory into a structure
allFilesInDirectory = dir;
% Initialise indexes for files that do and do not contain oldString
filesWithStringIndex = 1;
filesWithoutStringIndex = 1;
% For the number of files and folders in the directory
for idx = 1 : length(allFilesInDirectory)
% If the file name contains any of the English character set (letters, numbers or
% underscore character i.e. a-zA-Z_0-9) and ends with a ".m" filetype...
if (~isempty ( regexp(allFilesInDirectory(idx).name, '[\w]+\.m','match') ))
% Open the file for reading
fileIdRead = fopen(allFilesInDirectory(idx).name, 'r');
% Extract the text
fileText = fscanf(fileIdRead,'%c');
% Close the file
fclose(fileIdRead);
% Search for occurrences of oldString
occurrences = strfind(fileText,oldString);
% If an occurrence is found...
if ~isempty(occurrences)
% Replace any occurrences of oldString with newString
fileTextNew = strrep(fileText, oldString, newString);
% Open the file for writing
fileIdWrite = fopen(allFilesInDirectory(idx).name, 'w');
% Write the modified text
fprintf(fileIdWrite, '%c', fileTextNew);
% Close the file
fclose(fileIdWrite);
% Update the list of files that contained oldString
filesWithString{filesWithStringIndex} = allFilesInDirectory(idx).name;
% Update the index for files that contained oldString
filesWithStringIndex = filesWithStringIndex + 1;
else
% Update the list of files that did not contain oldString
filesWithoutString{filesWithoutStringIndex} = allFilesInDirectory(idx).name;
% Update the index for files that did not contain oldString
filesWithoutStringIndex = filesWithoutStringIndex + 1;
end
end
end
%% Display what files were changed, and what were not
% If the variable filesWithString exists in the workspace
if exist('filesWithString','var')
disp('Files that contained the target string that were updated:');
% Display their names
for i = 1:filesWithStringIndex-1, disp(filesWithString{i}); end
else
disp('No files contained the target string');
end
% Insert a clear line between lists
disp(' ');
% If the variable fileWithoutString exists in the workspace
if exist('filesWithoutString','var')
disp('Files that were not updated:');
% Display their names
for j = 1:filesWithoutStringIndex-1, disp(filesWithoutString{j}); end
else
disp('All files contained the target string.');
end