【问题标题】:How to read, and perform a certain command for multiple files for multiple different folders如何为多个不同文件夹的多个文件读取和执行某个命令
【发布时间】:2015-02-08 03:31:43
【问题描述】:

所以我需要为多个不同的文件夹解压缩大量文件(通过 gzip 命令),比如说文件夹 1 到 200(命名文件夹 1 到文件夹 200)。然后,这些文件夹(1 到 200)位于其他父文件夹中:Parentfolder1-Parentfolder200,比方说。结构将是这样的:

Parentfolder1 --> folder1 --> 文件,将全部解压缩 (gzip *.gz)
父文件夹 1 --> 文件夹 2 --> 文件
.
.
.
父文件夹 1 --> 文件夹 200 --> 文件
父文件夹 2 --> 文件夹 1 --> 文件
.
.
Parentfolder200 --> folder200 --> 文件


我在从一个文件夹导航到另一个文件夹以及从 Parentfolder 导航到 Parentfolder 时遇到了一些困难,以便使用 dir 命令通过简单的 do 循环执行必要的命令。实现这一目标的最佳方法是什么?提前谢谢你。

【问题讨论】:

  • .gz 类型的每个文件夹中的所有文件吗?还是需要根据扩展名选择合适的文件?
  • 是的,每个文件都有扩展名.gz,并且是文件夹中唯一的文件

标签: matlab file


【解决方案1】:

对于这种类型的任务,我喜欢使用fileattrib。该函数的输入是父文件夹名称。如果您以\* 结束该名称,则fileattrib 函数递归地 工作并返回文件和文件夹的所有层次结构(可能需要一些时间)。至少在 Windows 中是这样的。

所以你可以使用这些方面的东西。在以下代码中,应用于每个文件的处理仅包括显示其全名(路径);在您的情况下,您将应用所需的命令。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    相关资源
    最近更新 更多