0
votes

I have 500 wave files in a folder ABC which are named like

F1001
F1002
F1003
...
F1100
F2001
F2002
...
F2100
M3001
M3002
...
M3100
M4001
M4002
...
M4100

all with extension .wav.

Also I have a text file which contains 3 digit numbers like

  1. 001
  2. 003
  3. 098
  4. 034 .... (200 in total).

I want to select wave files from the folder ABC whose names end with these 3 digits.

Expecting MATLAB or bash script solutions.

I read this: Copy or move files to another directory based on partial names in a text file. But I don't know how to use it for me.

3

3 Answers

1
votes

for Matlab

1) Get all the file names in the folder using functions dir or rdir.

2) Using for loop go through every filename and add the last 3 digits of every filename to an array (array A). You will need str2num() here

3) Parse all 3 digit numbers to an array (array B)

4) Using function ismember(B, A) find which elements of B are contained in A

5) Load corresponding filenames

0
votes
find . -name "*.wav" | grep -f <(awk '{print $0 ".wav"}' file)

grep -f will use the patterns stored in file, one per line, and look for them in your find result. But you want the three numbers to be at the end, so in the above command the last awk statement will provide a modified file with ".wav" appended at each line. So for the line 001, "0001.wav" will match but any file 0010.wav will not.

see: process substitution syntax and grep --help

0
votes
function wavelist()

   wavefiles=dir('*.wav');   % loaded wave files
   myfolder='/home/adspr/Desktop/exps_sree/waves/selectedfiles'; %Output folder to store files

   for i=1: numel(wavefiles)  %for each wave file
      filename=wavefiles(i).name;  
      [~,name,~] = fileparts(filename);  % found the name of file without extension
      a=name(end-2:end); %get the last 3 digits in the file name
      fileID = fopen('nameslist','r');

      while ~feof(fileID)
          b=fgetl(fileID); % get each line in the list file

          if strcmp(a,b) % compare 
              movefile(filename,myfolder); % moved to otput folder

          end

      end
   fclose(fileID); 

   end
end

I don't think this is a simple answer, thats why I asked here. Anyway, my problem solved, thats why I posted this as an answer.

Thank you all.