How to copy a file to multiple folders using the command line?
    2 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    viet le
 il 30 Dic 2016
  
    
    
    
    
    Commentato: Image Analyst
      
      
 il 2 Gen 2017
            I have tried to copy a file test.txt to multiple directories with one command:
for ii=0:5:175
for jj=0:3:90
newdir=sprintf('phi=%2dtheta=%2d',ii,jj);
mkdir(fullfile(newdir))
end
end
for ii=0:5:175
    for jj=0:3:90
       source_dir=sprintf('G:/SYSTEM MATRIX - MCNP/phi%2dtheta%2d',ii,jj);
        copyfile('test.txt','source_dir')
 end
end
But I didn't succeed. Is there a way to do that in one command so I can copy a file or even a folder to multiple directories?
0 Commenti
Risposta accettata
  Image Analyst
      
      
 il 30 Dic 2016
        What does "didn't succeed" mean? Did it finish with no errors but there were no files there? Or did it give an error message? copyfile() takes the destination folder as the second argument, not the source folder. As if that wasn't bad enough, you're passing the string 'sourcedir' instead of sourcedir so it's not even passing the string you want it to. To fix, get rid of the single quotes. And make sure all folders exist before you call copy file. exist(folder, 'dir') is useful for that purpose.
4 Commenti
  Image Analyst
      
      
 il 2 Gen 2017
				Create the destination folder in a loop
baseFileName = 'whatever.dat';
inputFullFileName = fullfile(inputFolder, baseFileName);
for k = 1 : 1000
    outputFolder = sprintf('...........whatever
    if ~exist(outputFolder, 'dir')
        mkdir(outputFolder);
    end
    outputFullFileName = fullfile(outputFolder, baseFileName);
    copyfile(inputFullFileName, outputFullFileName);
end
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su File Operations in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

