How to move files with name containing 1,7,13, 1+6n to a new folder?

2 次查看(过去 30 天)
Hi I have hundreds of files with the same names except the number in a folder,for example 2018test001.nd2, 2018test002.nd2, 2018test003.nd2,....2018test384.nd2. I want to move files with name containing 1,7,13,...1+6n to 1st new folder, and files with name containing 2,8,14,....2+6n to 2nd new folder,.....until files with name containing 6, 12,18,....6+6n to 6th new folder.
I have no clue how to perform it and hope to get some help. Thank you very much!

采纳的回答

Guillaume
Guillaume 2018-11-8
I have no clue how to perform it
Well, neither do we until we start to think about the problem, deconstruct it into steps and think on how to solve each step. You usually don't inherently know how to solve a problem. You need to engage your brain.
In your case, clearly, you want to get the list of files, get the number at the end of file, divide that number by 6 and take the remainder, and move the file into the corresponding folder according to that remainder. Once you've written down the tasks like this, it's just a matter of writing the equivalent in code. It's going to be something like:
filebase = 'something';
fileextenstion = 'ext';
sourcefolder = 'c:\somewhere\somefolder';
destinationprefix = 'c:\somewhere\someotherfolder';
for fidx = 0:5
mkdir(sprintf('%s%d', destinationprefix, fidx)); %create destination folders
end
filelist = dir(fullfile(folder, sprintf('%s*.%s', filebase, fileextension))); %get list of files
filenumber = sscanf([filelist.name], sprintf('%s\%d.%s', filebase, fileextension)) %get the file numbers
foldernumber = mod(filenumber, 6);
for fidx = 1:numel(filelist)
movefile(fullfile(folder, file(fidx).name), fullfile(sprintf('%s%d', destinationprefix, foldernumber(idx)), file(fidx).name);
end
  4 个评论
Shirley
Shirley 2018-11-12
编辑:Shirley 2018-11-12
So this is exactly the format
20181108_AB test on MCE p53Venus001.nd2
20181108_AB test on MCE p53Venus002.nd2
20181108_AB test on MCE p53Venus003.nd2
...etc
nd2 is the extension. I just figured out how to extract the numbers 001, 002, etc by using
regexp(s,'(?<=s)\d{3}','match','once')
But I am now stuck at moving the files to corresponding destionation folders.
Again, sorry for the confusion. Thanks!
Shirley
Shirley 2018-11-12
HIIIIIIII! It works now!!
movefile(fullfile(sourcefolder, files(fidx).name), fullfile(sprintf('%s%d', destinationprefix, foldernumber(fidx)), files(fidx).name));
There were just some f and source missing in your code. Thank you so much!!! I am so excited. :)

请先登录,再进行评论。

更多回答(3 个)

madhan ravi
madhan ravi 2018-11-8

Image Analyst
Image Analyst 2018-11-8
Give an example of the full filename. I don't know if the number is in the base filename, like abc6.dat, or if it's in the extension, like abc.6. Anyway, try using the function endsWith() to determine which files to copy, and movefile() to do the actual moving.
  1 个评论
Shirley
Shirley 2018-11-8
编辑:Shirley 2018-11-8
That is a good catch. Sorry for the confusion. So the full filenames are like test001.nd2, test002.nd2, test003.nd2,....test384.nd2. Thanks

请先登录,再进行评论。


Vasileios Tsiogkas
编辑:Vasileios Tsiogkas 2020-2-3
i would like to ask you somthing more in this case.
i have 18000 files named :
Tumble_1000RPM_WOT000000.T000.D000.P000.H000.L.vec
Tumble_1000RPM_WOT000001.T000.D000.P000.H000.L.vec
...........
Tumble_1000RPM_WOT017999.T000.D000.P000.H000.L.vec
i would like to seperate this files to 360 folders
in first forlder i would like to save the 1, 361, 721, 1081,1441,1801....
in second folder 2.362,722,1082,1442,1802...
in 360 folder the 360,720,1080,1440,1800...
and i use this code above....
clear all
clc
filebase = 'C:\Users\user\Desktop\2D_TR_PIV_2020\Tumble_Plane_1000RPM_WOT';
fileextenstion = 'vec';
sourcefolder = 'C:\Users\user\Desktop\2D_TR_PIV_2020\Tumble_Plane_1000RPM_WOT';
destinationprefix = 'C:\Users\user\Desktop\2D_TR_PIV_2020\Tumble_Plane_1000RPM_WOT\Tumble_Plane_1000RPM_WOT_results_320-2x';
for fidx = 0:360
mkdir(sprintf('%s%d', destinationprefix, fidx)); %create destination folders
end
filelist = dir(fullfile(sourcefolder, sprintf('%s*.%s', filebase, fileextenstion))); %get list of files
filenumber = sscanf([filelist.name], sprintf('%s\%d.%s', filebase, fileextenstion)); %get the file numbers
foldernumber = mod(filenumber, 360);
for fidx = 1:numel(filelist)
movefile(fullfile(folder, file(fidx).name), fullfile(sprintf('%s%d', destinationprefix, foldernumber(idx)), file(fidx).name));
end
but this isnt work.
Can you help me with this?

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by