1

I want to use R to organize (sort/move) over 1000 photos into 65 different site-specific folders. I have a csv file that contains the photo ID and the folder that it should go in. I used the csv file to create all the folders I need, which was pretty simple (see below), but I am lost on what to do next to actually move the photos to the appropriate folders.

for (i in 1:length(tracts)) {
    dir.create(file.path(photo_directory, tracts[i]))
}

There appears to be many ways to go about it, such as lapply, by, for loops, etc. Thanks in advance!

A_Weeks
  • 11
  • 3
  • Does [this SO answer](http://stackoverflow.com/a/10268255/496488) help? – eipi10 Apr 07 '16 at 19:38
  • `file.copy()` and friends (see `?files`) may be the next step. I suggest to save a copy of your original folder before... If you managed until here, you can survive to this ;-) – Vincent Bonhomme Apr 07 '16 at 19:39
  • assuming that you are using Windows, you might want to use system commands via shell(paste("move", srcFilePath, destPath)) assuming there are no space in srcFilePath and destPath. Otherwise escape with "\"". – chinsoon12 Apr 08 '16 at 01:19
  • I have looked at that code you suggested @eipi10, as well as the ?files, and these do not seem to work for what I need. I need to move the photos to many different directories, and it looks like the file.copy only allows me to move to one directory. Please correct me if I'm wrong. Also, my csv file (table) indicates which folder each photo has to go to, and I am not sure how to reference the table. – A_Weeks Apr 14 '16 at 19:58
  • Your csv file has the file name of the photo and the directory it should go in. So can't you just do a for loop that does `file.copy` on the file named column 1, copying it to the directory named in column 2? – eipi10 Apr 14 '16 at 20:06

1 Answers1

2

Assuming all the photos are in a directory with path pathToDir1 and your csv file (which we'll call df) has file names in column 1 and target directory in column 2, couldn't you do this:

for (i in 1:nrow(df)) {
  file.copy(from=paste0(pathToDir1, df[i,1]), to=paste0(df[i,2],"/", df[i,1]))
}
eipi10
  • 91,525
  • 24
  • 209
  • 285