renaming certain part of a file

5 visualizzazioni (ultimi 30 giorni)
Rama Afana
Rama Afana il 27 Mag 2019
Commentato: Rama Afana il 28 Mag 2019
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

Risposte (2)

KSSV
KSSV il 27 Mag 2019
Modificato: KSSV il 27 Mag 2019
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 Commenti
KSSV
KSSV il 27 Mag 2019
Are you in the folder where text files are present?
Rama Afana
Rama Afana il 27 Mag 2019
yes the .m file are in the same folder with text files. do I have to move it ?

Accedi per commentare.


per isakson
per isakson il 27 Mag 2019
Modificato: per isakson il 27 Mag 2019
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 Commenti
per isakson
per isakson il 27 Mag 2019
Modificato: per isakson il 27 Mag 2019
"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 il 28 Mag 2019
oke thanks a lot for your help

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by