Does anyone have a Matlab code that would delete part of filenames in many directories?

2 ビュー (過去 30 日間)
I have a large number of waveform records that have been saved as individual files in many directories.
The directory structure is \RawData\Cell01, \RawData\Cell02, \RawData\Cell03...\RawData\Cell99
The data files in each of these subdirectories are named Cellxx_00000_xxxxx.xxx except the first file which is Cellxx_00000.dat
For example I have a subdirectory \RawData\Cell01 that may contain several thousand files named Cell01_00000_00001.dat, Cell01_00000_00002.dat; Cell01_00000_00003.dat… Cell01_00000_04300.dat.
I would like to:
  1. remove the characters "_00000" from these files and rename them as: Cell01_00001.dat, Cell01_00002.dat, Cell01_00003.dat ... Cell01_04300.dat, and
  2. copy the renamed files to subdirectory \RawData\Renamed\Cell01. The file Cell01_00000.dat must not be changed.
Does anyone have a code that could perform this task?
  5 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 19 日
I have edited the code to fix the reference to filename
Vahe Ghazikhanian
Vahe Ghazikhanian 2019 年 1 月 19 日
Great...Thanks a lot. It is working now.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 1 月 17 日
編集済み: Walter Roberson 2019 年 1 月 19 日
Given a filename
newfilename = regexprep(filename, '000000_', '');
The rest is just finding files and copyfile()
The below needs R2016b or later
projectdir = 'RawData';
outdir = fullfile(projectdir, 'Renamed');
if ~exist(outdir, 'dir'); mkdir(outdir); end
dinfo = dir( fullfile(projectdir, 'Cell*', '*.dat') );
fullnames = fullfile( {dinfo.folder}, {dinfo.name} );
for K = 1 : length(fullnames)
thisfile = fullnames{K};
[fulldir, basename, ext] = fileparts(thisfile);
[parentdir, basedir] = fileparts(fulldir);
newfilename = [regexprep(basename, '000000_', '', 'once'), ext];
newdir = fullfile(outdir, basedir);
if ~exist(newdir, 'dir'); mkdir(newdir); end
newfilename = fullfile(newdir, newfilename);
copyfile(thisfile, newfilename)
end
  2 件のコメント
Vahe Ghazikhanian
Vahe Ghazikhanian 2019 年 1 月 18 日
Thanks for the code. What does the string 'first" in the regexprep(filename, '000000_', '', 'first'), do? I could not find this under the possible options for regexprep().
Walter Roberson
Walter Roberson 2019 年 1 月 18 日
Sorry, should be 'once' not 'first'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by