简体   繁体   中英

How to read, and perform a certain command for multiple files for multiple different folders

So I need to unzip a plethora of files (via the gzip command) for multiple different folders, lets say folders 1 through 200 (names folder 1 through folder 200). These folders (1 through 200) are then located within other parent folders: Parentfolder1-Parentfolder200, lets say. The structure would go as such:

Parentfolder1 --> folder1 --> files, which will all be uncompressed (gzip *.gz)
Parentfolder1 --> folder2 --> files
.
.
.
Parentfolder1 --> folder200 --> files
Parentfolder2 --> folder1 --> files
.
.
Parentfolder200 --> folder200 --> files


I am having some difficulty navigating from folder to folder, and Parentfolder to Parentfolder to perform the command necessary with a simple do loop using the dir command. What would be the best way to achieve this? Thank you in advance.

For this type of task I like to use fileattrib . The input to that function is the parent folder name. If you end that name with \\* , the fileattrib function works recursively and returns all the hierarchy of files and folders (may take some time). At least that's what happens in Windows.

So you could use something along these lines. In the following code the processing applied to each file consists only in displaying its full name (path); in your case you will apply the desired command.

root_folder = 'your\root\folder'; %// root folder of all files you want to process
extension = '.gz'; %// process only files whose name ends with this string
e = numel(extension);
[status, files] = fileattrib([root_folder '\*']); %// add '\*'
for n = 1:numel(files)
    if ~files(n).directory &&...
        numel(files(n).Name)>=e &&...
        all(files(n).Name(end-e+1:end)==extension)
        %// file(n) is of the desired type. Do domething with it.
        %// Its full name (path) is given by files(n).Name
        disp(files(n).Name) %// in this example we only display its full name
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM