フィルターのクリア

How to copy and rename files in a different folder

265 ビュー (過去 30 日間)
012786534
012786534 2017 年 1 月 28 日
編集済み: jonathan 2017 年 10 月 20 日
Hi all,
Let's say I have 3 text files (001X, 002Y and 003Z) in the folder A. Now, I want to first move these files to a created folder B and then rename them like so: 001_T, 002_T and 003_T) so I now have the old files in folder A and the new renamed ones in folder B. I'm almost done but my code (below) doesn't quite work. Any ideas? Thank you.
%First create the folder B
CurrentDirectory=pwd;
if exist([pwd '\B'])~=7
mkdir B
end
%Move the files with copyfiles ?
%Then rename the files
A =dir( fullfile('*.txt') );
fileNames = { A.name };
for iFile = 1 : numel( A )
movefile(A(iFile).name,[A(iFile).name(1:3) '_T.txt']);
end

採用された回答

Image Analyst
Image Analyst 2017 年 1 月 29 日
Try this:
% First create the folder B, if necessary.
outputFolder = fullfile(pwd, 'B')
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
% Copy the files over with a new name.
inputFiles = dir( fullfile('*.txt') );
fileNames = { inputFiles.name };
for k = 1 : length(inputFiles )
thisFileName = fileNames{k};
% Prepare the input filename.
inputFullFileName = fullfile(pwd, thisFileName)
% Prepare the output filename.
outputBaseFileName = sprintf('%s_T.txt', thisFileName(1:end-4));
outputFullFileName = fullfile(outputFolder, outputBaseFileName)
% Do the copying and renaming all at once.
copyfile(inputFullFileName, outputFullFileName);
end

その他の回答 (3 件)

Image Analyst
Image Analyst 2017 年 1 月 28 日
You need to take the badly-named B folder and make the full filename out of it
outputBaseFileName = [A(iFile).name(1:3) '_T.txt'];
outputFileName = fullfile(B, outputBaseFileName );
and use that in movefile.
  1 件のコメント
012786534
012786534 2017 年 1 月 28 日
Sorry, but Im not sure I follow you, quite frankly

サインインしてコメントする。


Michael Rowlands
Michael Rowlands 2017 年 7 月 2 日
編集済み: Michael Rowlands 2017 年 7 月 2 日
I have encountered similar situations where I need to copy and rename files. So I wrote a function to handle many copy and rename configurations. It's a convenient way of copying and renaming large groups of files using lists and wildcards. It's called "easycopy" and is available on the Matlab File Exchange.
(There's also a sister function, "easyrename" which does the same searching and find-replace, but with a move/rename command.)
Here's how it works in the exact situation described by bluegin.
ORIGINAL DIRECTORY: ...copy2\001X.txt, ...002Y.txt, ...003Z.txt
NEW DIRECTORY: ....copy2\newdir
easycopy('c:\USERN\desktop\copy2\00??.*','c:\USERN\desktop\copy2\newdir\00?_T.*')
COPYING FILES .....
Copying c:\USERN\desktop\copy2\001X.txt
To c:\USERN\desktop\copy2\newdir\001_T.txt
Copying c:\USERN\desktop\copy2\002Y.txt
To c:\USERN\desktop\copy2\newdir\002_T.txt
Copying c:\USERN\desktop\copy2\003Z.txt
To c:\USERN\desktop\copy2\newdir\003_T.txt
DONE !

jonathan
jonathan 2017 年 8 月 30 日
編集済み: jonathan 2017 年 10 月 20 日
You can use code by Image analyst or you can easily use Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org that allows you to quickly rename all the files created in folder A to folder B.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by