Changing file names based on their filetype extension
2 次查看(过去 30 天)
显示 更早的评论
I don't know if I can use MATLAB for this or something like terminal or cmd would be better but can someone tell me
I have a folder with name say Videos and inside this folder I have multiple files: one mp4, one ppt, one pdf, and one doc. The video name is say "Movieking1-1" and all other file types have name as "sample". Now what I would like is a set of commands which searches for all the extensions like *.pdf and *.docs in the folder and give it the name of that as the one for the .mp4 and add -yt So if my folder was like this:
Movieking1-1.mp4
sample.docx
sample.pdf
The command should work and change the name for the file in that folder as
Movieking1-1.mp4
Movieking1-1-transcript.docx
Movieking1-1-transcript.pdf
There are no other docs or pdfs either in the same folder. I am asking help because i have to do this for more than 100 folders.
Any help is appreciated Thanks in advances
0 个评论
回答(1 个)
Stephen23
2018-10-17
编辑:Stephen23
2018-10-17
D = 'path to the parent directory where the folders are';
S = dir(fullfile(D,'*'));
S = S([S.isdir]);
for k = 1:numel(S)
T = dir(fullfile(D,S(k).name,'*.mp4'));
[~,N] = fileparts(T.name);
% For PDF:
old = fullfile(D,S(k).name,'sample.pdf');
new = fullfile(D,S(k).name,sprintf('%s-transcript.pdf',N));
movefile(old,new)
% Repeat for DOCX, etc.
end
2 个评论
Stephen23
2018-10-18
" What am I doing wrong?"
You used addpath. Does my answer use addpath anywhere? No, it does not. addpath is a totally inappropriate tool for reading data files. The MATLAB Search Path is for specifying where MATLAB looks for CODE, not for filling it up with any old folder that contains data files.
Specify the D as a simple character vector with an absolute/relative folder path, or use uigetdir:
D = uigetdir();
Do not use addpath for handling data files: always use absolute/relative filenames.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!