Does this work to parse through folders and take out the contents and remove the folder?

2 views (last 30 days)
% Define the input directory
inputDirectory = '/path/to/your/directory'; % Change this to your desired directory
% List of file extensions to match
fileExtensions = {'.cs', '.tdm', '.rt', '.xm', '.tdms_inde', '.tx', '.mp', '.ch'};
% Get a list of all items (files and directories) in the input directory
allItems = dir(inputDirectory);
% Initialize an empty cell array to store matching directories
matchingDirectories = {};
% Iterate through each item in the directory
for i = 1:numel(allItems)
currentItem = allItems(i);
% Check if the item is a directory and its name ends with one of the specified extensions
if currentItem.isdir
[~, name, ext] = fileparts(currentItem.name);
if any(strcmpi([ext, name], fileExtensions))
matchingDirectories = [matchingDirectories, {currentItem.name}];
end
end
end
% Display the list of matching directories
fprintf('Matching directories in %s:\n', inputDirectory);
for i = 1:numel(matchingDirectories)
fprintf('%s\n', fullfile(inputDirectory, matchingDirectories{i}));
% Get the source and destination paths
sourceDir = fullfile(inputDirectory, matchingDirectories{i});
destinationDir = inputDirectory;
% Copy the contents of the source directory to the destination directory
copyfile(sourceDir, destinationDir);
% Delete the source directory
rmdir(sourceDir, 's');
end
fprintf('All matching directories have been processed.\n');
  15 Comments
Jordan
Jordan on 12 Sep 2023
To achieve this in MATLAB, you can use the following code. This code will:
1. Prompt the user for an input directory.
2. List all directories within the input directory that fall between June 12, 2023, and the current date.
3. Search for "66666" in the "testresults.xml" file within each of these directories.
4. If "66666" is found, it will create a folder in "D:\TerryRan\" with the same name as the subdirectory and copy the "testresults.xml" file into that folder.
```matlab
% Get the input directory from the user
inputDirectory = input('Enter the directory path: ', 's');
% Check if the input directory exists
if ~isfolder(inputDirectory)
fprintf('The specified directory does not exist.\n');
return;
end
% Define the date range (June 12, 2023, to today)
startDate = datetime('2023-06-12');
endDate = datetime('now');
% List all subdirectories in the input directory
subDirectories = dir(inputDirectory);
subDirectories = subDirectories([subDirectories.isdir]); % Filter out non-directories
% Loop through each subdirectory
for i = 1:numel(subDirectories)
subDir = subDirectories(i).name;
% Skip '.' and '..' directories
if strcmp(subDir, '.') || strcmp(subDir, '..')
continue;
end
subDirPath = fullfile(inputDirectory, subDir);
% Check if the modification date of the subdirectory is within the date range
dirInfo = dir(subDirPath);
modificationDate = datetime(dirInfo.date, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss');
if modificationDate >= startDate && modificationDate <= endDate
% Search for "66666" in the testresults.xml file
xmlFilePath = fullfile(subDirPath, 'testresults.xml');
if exist(xmlFilePath, 'file')
xmlData = fileread(xmlFilePath);
if contains(xmlData, '66666')
% Create a folder in D:\TerryRan\
newFolder = fullfile('D:\TerryRan', subDir);
mkdir(newFolder);
% Copy the testresults.xml file to the new folder
copyfile(xmlFilePath, fullfile(newFolder, 'testresults.xml'));
fprintf('Copied testresults.xml from %s to %s.\n', subDirPath, newFolder);
end
end
end
end
```
Make sure to replace `'D:\TerryRan'` with the desired destination folder path. This code should perform the described actions for each subdirectory that meets the initial criteria.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 11 Sep 2023
if currentItem.isdir
[~, name, ext] = fileparts(currentItem.name);
if any(strcmpi([ext, name], fileExtensions))
That code does not look right.
The ext portion of fileparts output will be empty char vector, '', if the terminal filename has no period in it. [ext, name] would then be the same as name and you would be comparing the name to fileExtensions, but fileExtensions all start with period and if name contained any period then ext would not be empty. So if the file name has no extension then it cannot match any entry in fileExtensions
If the file name contained at least one period then ext will start with the period . [ext, name] would put the file extension followed by the name, giving you something that starts with a period. This does have the potential to match entries in fileExtensions . However, it implies that you would want to match if you had a directory named s.c or a directory named .cs but not if you had a directory named abc.cs as [ext, name] for abc.cs would get transformed to '.csabc' which cannot match.
It would be so odd that if you really did want to match directories named .cs or s.c but not with extension .cs, that you would really have to document your logic

Jordan
Jordan on 5 Oct 2023

% Define the root directory to search in rootDirectory = 'D:/test results'; fileToSearch = 'progress pane.doc';

% Initialize an empty list to store directory names where "violate" is found clcViolateList = {};

% Function to recursively search for the file and add directory names to the list function searchDirectory(directory) files = dir(fullfile(directory, fileToSearch)); for i = 1:numel(files) filePath = fullfile(directory, files(i).name); fileText = fileread(filePath); if contains(fileText, 'violate') clcViolateList{end+1} = directory; break; % Once "violate" is found in a directory, no need to check further end end

    subdirs = dir(directory);
    subdirs = subdirs([subdirs(:).isdir]);
    subdirs = subdirs(~ismember({subdirs(:).name}, {'.', '..'}));
    for i = 1:numel(subdirs)
        searchDirectory(fullfile(directory, subdirs(i).name));
    end
end

% Start the search from the root directory searchDirectory(rootDirectory);

% Define the output directory and file outputDir = 'D:/Violate'; outputFile = 'CLCViolate.txt'; outputFilePath = fullfile(outputDir, outputFile);

% Create the directory if it doesn't exist if ~isfolder(outputDir) mkdir(outputDir); end

% Write the list of directory names to the text file fid = fopen(outputFilePath, 'w'); fprintf(fid, '%s\n', unique(clcViolateList{:})); fclose(fid);

  1 Comment
Jordan
Jordan on 4 Jan 2024
Function LineOfBestFit(X() As Double, Y() As Double) As (Double, Double)
' Check if the input arrays have the same length
If UBound(X) <> UBound(Y) Then
MsgBox "Input arrays must have the same length"
Exit Function
End If
' Calculate the necessary sums
Dim n As Integer
Dim sumX As Double, sumY As Double, sumXY As Double, sumX2 As Double
n = UBound(X) + 1
For i = 0 To UBound(X)
sumX = sumX + X(i)
sumY = sumY + Y(i)
sumXY = sumXY + X(i) * Y(i)
sumX2 = sumX2 + X(i) ^ 2
Next i
' Calculate the slope (m) and intercept (b) of the line of best fit
Dim slope As Double, intercept As Double
slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX ^ 2)
intercept = (sumY - slope * sumX) / n
' Return the result as a tuple
LineOfBestFit = (slope, intercept)
End Function

Sign in to comment.

Categories

Find more on System Commands in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!