renaming certain part of a file

6 次查看(过去 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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by