How to select files based on the name?

3 views (last 30 days)
Andi
Andi on 12 Jan 2022
Answered: Rik on 12 Jan 2022
My dataset consists of about 6000 txt files. Every three files have same initial name but diffrent last name.I want to write a code that first select all the three files has same initial name and then combine them in one file and delete them from directory at the same time. This process repeats onward. In the end I will left with 2000 files only.
For example, 123x.txt, 123y.txt, 123z.txt..... it be one group and so on. 123.txt
  4 Comments
Andi
Andi on 12 Jan 2022
Apology! I used 123 just for understanding, the orginla name of the files are like
CHICH_20190924_114203.Z.sac
CHICH_20190924_110755.Y.sac
CHICH_20190924_114335.Y.sac
CHICH_20190924_114203.X.sac
CHICH_20190924_113637.Y.sac
CHICH_20190924_110755.X.sac
CHICH_20190924_114335.Z.sac
CHICH_20190924_113637.X.sac
CHICH_20190924_114335.X.sac
CHICH_20190924_114203.Y.sac
CHICH_20190924_113637.Z.sac
CHICH_20190924_110755.Z.sac

Sign in to comment.

Answers (1)

Rik
Rik on 12 Jan 2022
First you need to retrieve all file names with dir. Then you can split the file name on the dots with the split function. After that you can use the unique function and generate the 3 source file names and the new file name.
The merging itself depends on the specifics of your files.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by