Changing file names based on their filetype extension

2 vues (au cours des 30 derniers jours)
Ullas Rajvanshi
Ullas Rajvanshi le 17 Oct 2018
Commenté : Stephen23 le 18 Oct 2018
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

Réponses (1)

Stephen23
Stephen23 le 17 Oct 2018
Modifié(e) : Stephen23 le 17 Oct 2018
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 commentaires
Ullas Rajvanshi
Ullas Rajvanshi le 17 Oct 2018
when i add the path for D it gives an error and doesnt read fullfile. What am I doing wrong?
D = addpath('/Users/UR/Desktop/Sample/Course_2018_')
The files are in folder called Course_2018_ but then I get this error:
Error using eval
Undefined variable "matlab" or class "matlab.internal.editor.eval.TmpFilePath.resetTempFolder".
Error in matlab.internal.editor/evaluateCode
Error in matlab.internal.editor/evaluateCode
Undefined function or variable 'matlab.unittest.internal.ui.toolstrip.getFileInfoForToolstrip'.
Undefined function or variable 'internal.matlab.desktop.editor.getSystemObjectInfo'.
Stephen23
Stephen23 le 18 Oct 2018
" 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by