renaming certain part of a file

5 ビュー (過去 30 日間)
Rama Afana
Rama Afana 2019 年 5 月 27 日
コメント済み: Rama Afana 2019 年 5 月 28 日
hello,
I have a question about renaming certain part of a file
I have several files as following
LM.BARI..BHZ.D.2018.246.000000.txt
LM.BARI..BHZ.D.2018.247.000000.txt
LM.BARI..BHZ.D.2018.248.000000.txt
LM.TOGK..BHZ.D.2018.246.000000.txt
LM.TOGK..BHZ.D.2018.247.000000.txt
LM.TOGK..BHZ.D.2018.248.000000.txt
I have 350 files and I would like to rename all those files as
BARI_Z_246.txt
BARI_Z_247.txt
BARI_Z_248.txt
TOGK_Z_246.txt
TOGK_Z_247.txt
TOGK_Z_248.txt
could anyone help me ?
thanks

回答 (2 件)

KSSV
KSSV 2019 年 5 月 27 日
編集済み: KSSV 2019 年 5 月 27 日
files = dir('*.txt') ;
for i = 1:length(files)
V = str2double(regexp(files(i).name,'\d+','match')) ;
s = ['BARI_Z_',num2str(V(2)),'.txt'] ;
movefile(files(i).name,s)
end
  5 件のコメント
KSSV
KSSV 2019 年 5 月 27 日
Are you in the folder where text files are present?
Rama Afana
Rama Afana 2019 年 5 月 27 日
yes the .m file are in the same folder with text files. do I have to move it ?

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


per isakson
per isakson 2019 年 5 月 27 日
編集済み: per isakson 2019 年 5 月 27 日
Try this
%%
xpr = '^[^.]+\.([^.]+)\.{2}\w+?(\w)\.\w+\.\d{4}\.(\d+)\.';
%%
old_name = 'LM.BARI..BHZ.D.2018.246.000000.txt';
cac = regexp( old_name, xpr, 'tokens' );
new_name = sprintf( '%s_%s_%s.txt', cac{1}{:} )
%%
old_name = 'LM.TOGK..BHZ.D.2018.248.000000.txt';
cac = regexp( old_name, xpr, 'tokens' );
new_name = sprintf( '%s_%s_%s.txt', cac{1}{:} )
it outputs
>> Untitled4
new_name =
'BARI_Z_246.txt'
new_name =
'TOGK_Z_248.txt'
>>
This appears to work, however will all 350 file-names honor the pattern, xpr, I have deduced from your example?
I use the substrings marked with yellow to build the new name.
In response to comment
Make a backup of your text files!
Here is a working example (put this code in a file named rename_my_files.m and replace 'h:\m\cssm' by the name of the folder, in which you keep your files.)
%% Sample files
cmd_create_file( fullfile('h:\m\cssm','LM.BARI..BHZ.D.2018.246.000000.txt'));
cmd_create_file( fullfile('h:\m\cssm','LM.BARI..BHZ.D.2018.247.000000.txt'));
cmd_create_file( fullfile('h:\m\cssm','LM.BARI..BHZ.D.2018.248.000000.txt'));
cmd_create_file( fullfile('h:\m\cssm','LM.TOGK..BHZ.D.2018.246.000000.txt'));
cmd_create_file( fullfile('h:\m\cssm','LM.TOGK..BHZ.D.2018.247.000000.txt'));
cmd_create_file( fullfile('h:\m\cssm','LM.TOGK..BHZ.D.2018.248.000000.txt'));
%%
rename_my_files_( 'h:\m\cssm\LM*.txt' )
%%
function rename_my_files_( glob )
glob = fullfile( glob );
sad = reshape( dir( glob ), 1,[] );
xpr = '^[^.]+\.([^.]+)\.{2}\w+?(\w)\.\w+\.\d{4}\.(\d+)\.';
for s = sad
old_ffs = fullfile(s.folder,s.name);
cac = regexp( old_ffs, xpr, 'tokens' );
new_ffs = fullfile( s.folder, sprintf( '%s_%s_%s.txt', cac{1}{:} ) );
[ status, msg ] = movefile( old_ffs, new_ffs );
if status
fprintf( 1, 'Success: %s to %s\n', old_ffs, new_ffs )
else
fprintf( 2, 'Failure: %s, %s\n', old_ffs, msg )
end
end
end
Test run
>> rename_my_files
Success: h:\m\cssm\LM.BARI..BHZ.D.2018.246.000000.txt to h:\m\cssm\BARI_Z_246.txt
Success: h:\m\cssm\LM.BARI..BHZ.D.2018.247.000000.txt to h:\m\cssm\BARI_Z_247.txt
Success: h:\m\cssm\LM.BARI..BHZ.D.2018.248.000000.txt to h:\m\cssm\BARI_Z_248.txt
Success: h:\m\cssm\LM.TOGK..BHZ.D.2018.246.000000.txt to h:\m\cssm\TOGK_Z_246.txt
Success: h:\m\cssm\LM.TOGK..BHZ.D.2018.247.000000.txt to h:\m\cssm\TOGK_Z_247.txt
Success: h:\m\cssm\LM.TOGK..BHZ.D.2018.248.000000.txt to h:\m\cssm\TOGK_Z_248.txt
>>
  3 件のコメント
per isakson
per isakson 2019 年 5 月 27 日
編集済み: per isakson 2019 年 5 月 27 日
"how could I read all those files name and write it again to the files?"
My intention was that you should have tried to combine my "new_name" with KSSV's script. There is a mistake in KSSV's "new_name" code.
Rama Afana
Rama Afana 2019 年 5 月 28 日
oke thanks a lot for your help

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by