Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:按顺序重命名文件_Matlab_File_Rename_Sequential - Fatal编程技术网

Matlab:按顺序重命名文件

Matlab:按顺序重命名文件,matlab,file,rename,sequential,Matlab,File,Rename,Sequential,我有许多没有顺序的文本文件: 010010.txt 010030.txt 010070.txt 如何将文件名更改为: text01.txt text02.txt .... 是否可以不重写旧目录,而是创建一个新目录 我使用了以下脚本,但结果是它工作正常,但从text001.txt到text021.txt再到text041.txt 有什么想法吗 directory = 'C:\test\'; %//' Directory with txt files filePattern = fullfil

我有许多没有顺序的文本文件:

010010.txt 010030.txt 010070.txt
如何将文件名更改为:

text01.txt text02.txt ....
是否可以不重写旧目录,而是创建一个新目录

我使用了以下脚本,但结果是它工作正常,但从text001.txt到text021.txt再到text041.txt

有什么想法吗

directory  = 'C:\test\'; %//' Directory with txt files
filePattern = fullfile(directory, '*.txt'); %//' files pattern with absolute paths
old_filename = cellstr(ls(filePattern)) %// Get the filenames
file_ID = strrep(strrep(old_filename,'file',''),'.txt','') %// Get numbers associated with each file

file_ID_doublearr = str2double(file_ID)

file_ID_doublearr = file_ID_doublearr - min(file_ID_doublearr)+1

file_ID = strtrim(cellstr(num2str(file_ID_doublearr)))

str_zeros = arrayfun(@(t) repmat('0',1,t), 4-cellfun(@numel,file_ID),'uni',0) %// Get zeros string to be pre-appended to each filename

new_filename = strcat('file',str_zeros,file_ID,'.txt') %// Generate new filenames

cellfun(@(m1,m2) movefile(m1,m2),fullfile(directory,old_filename),fullfile(directory,new_filename)) %// Finally rename files with the absolute paths

这看起来很复杂。我只需进行一次系统调用,将所有文件移动到一个新目录,然后使用其他系统调用依次重命名每个文件。看起来您正在使用Windows,因此我将为该平台提供一个解决方案。您正是从源目录中读取文件的开始位置

directory  = 'C:\test\'; %// Directory with txt files
directoryToCopyOver = 'C:\out\'; %// Directory where you want to copy the files over

%// Copy source directory to target directory
system(['xcopy ' directory ' ' directoryToCopyOver]);

filePattern = fullfile(directoryToCopyOver, '*.txt'); %//' files pattern with absolute paths
names = dir(filePattern); %// Find all files with above pattern

%// For each file we have...
for idx = 1 : numel(names)
    name = names(idx).name; %// Get a name of a file
    %// Rename this file to textxx.txt
    outName = sprintf('text%2.2d.txt', idx);

    %// Call system and rename the file
    system(['ren ' directoryToCopyOver name ' ' directoryToCopyOver outName]);
end
需要注意的一些重要事项是,我使用它对Windows命令提示符进行系统调用。我习惯于将整个目录从一个点复制到另一个点。在这种情况下,这将是您的源目录到新的目标目录。在我这样做之后,我调用MATLAB来确定所有与您所布置的特定模式匹配的文件名,即所有文本文件

然后,对于我们拥有的每个文本文件名,我们读入这个名称,然后创建一个类型为textxx.txt的输出名称,其中xx是一个从1开始到我们拥有的文本文件数量的数字,然后我调用Windows命令提示符命令将文件从原始名称重命名为新名称。另外,看看MATLAB。它旨在使用格式分隔符创建字符串。如果你知道我是怎么称呼它的,%2.2d意味着我希望这个数字是两位数长的,如果这个数字少于两位数,请用零填充空格。如果您想增加位数,只需在每个位置添加更多。例如,如果希望有4位数字,请执行%4.4d,依此类推。这将正确地创建正确的字符串,以便我们可以在这个新目录中重命名正确的文件


希望这有帮助

这是可以做到的,但不要指望我们为你做所有的工作。显示您的尝试并询问有关您的具体问题,因此我刚刚添加了我一直在使用的脚本,但它从text001.txt跳到text021.txt,不知道为什么。