Ergodicity answered with great code, but:
- Some comments are written in German, not in English.
- There is a general lack of commentary, which makes understanding difficult.
I tried to fix these problems with the code below (I'm not sure if the MATLAB code is very well used using the language: lang-matlab, so paste it into MATLAB to make it easier to read):
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 ie 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 ie 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
source share