0

I have a list of files on different temporary paths in tempdir(). The paths are given here, where /tmp/Rtmp9pF0OF is my tempdir() for my R session.

[1] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/0"  "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/1" 
 [3] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/2"  "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/3" 
 [5] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/4"  "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/5" 
 [7] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/6"  "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/7" 
 [9] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/8"  "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/9" 
[11] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/10" "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/11"
[13] "/tmp/Rtmp9pF0OF/4fc42cb004a7160951778110/12" 

and the file names as in my local directory are given here

 "1.txt" "2.txt" "3.txt""4.txt" "GSM248238.CEL" "GSM248650.CEL"
 "GSM248651.CEL" "GSM248652.CEL" "GSM248653.CEL" "GSM248655.CEL" "GSM248659.CEL" "GSM248660.CEL""GSM248661.CEL"

I would like to manipulate the file paths in the tempdir() by changing the file names by the names given in above vector. say something like this.

[1] "/tmp/Rtmp9pF0OF/1.txt"          "/tmp/Rtmp9pF0OF/2.txt" 
[3] "/tmp/Rtmp9pF0OF/3.txt"          "/tmp/Rtmp9pF0OF/4.txt" 
[5] "/tmp/Rtmp9pF0OF/GSM248238.CEL"  "/tmp/Rtmp9pF0OF/GSM248650.CEL" 
[7] "/tmp/Rtmp9pF0OF/GSM248651.CEL"  "/tmp/Rtmp9pF0OF/GSM248652.CEL" 
[9] "/tmp/Rtmp9pF0OF/GSM248653.CEL"  "/tmp/Rtmp9pF0OF/GSM248655.CEL" 
[11]"/tmp/Rtmp9pF0OF/GSM248659.CEL" "/tmp/Rtmp9pF0OF/GSM248660.CEL"
[13]"/tmp/Rtmp9pF0OF/GSM248661.CEL" 

Thanks.

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • If you just want to concatenate `tempdir()` with `filenames`, you can use `file.path`: `file.path(tempdir(), filenames))`. – lukeA May 17 '15 at 10:32
  • @lukeA Does it contain the contents of the file also. – Agaz Wani May 17 '15 at 10:50
  • R if i can control the generation of the random file names also, that may also work. – Agaz Wani May 17 '15 at 10:51
  • @lukeA As the name of the first file in local directory is `1.txt` but on temporary path it gets changed to `4fc42cb004a7160951778110/0` and for other files also the same case. I can't call the file by its name as `1.txt` because the name `1.txt` does not exits on the temporary path. so i want to restore the names of the files on temporary paths. – Agaz Wani May 17 '15 at 11:05
  • You seem to have created temporary directories, not files. For example, to copy all files from your current working directory to your temporary directory, you could use `fns <- list.files(getwd(), full.names = TRUE); file.copy(from = fns, to = tempdir()); intersect(basename(fns), list.files(tempdir())) # check`. – lukeA May 17 '15 at 11:10
  • @lukeA let me give a try. Actually its an application , which has created the temporary files on temporary paths with random file names. so going through a hard time to call the files by original names. – Agaz Wani May 17 '15 at 11:31

1 Answers1

2

Here's one way how you could match the file names from one directory to those in the temporary directory by looking at the MD5 hashes:

# create sample data: 5 named files in working dir, 5 in temp dir
set.seed(1)
txts <- replicate(5, paste(sample(letters, 10, T), collapse = ""))
for (x in seq_along(txts)) {
  writeLines(txts[x], paste0(txts[x], ".txt"))
  writeLines(txts[x], tempfile(fileext = ".txt"))
}

# match file names from working and temp dir by MD5 hashes
library(tools)
src <- md5sum(list.files(getwd(), pattern = "^[a-z]{10}\\.txt$")) 
trg <- md5sum(list.files(tempdir(), full.names = TRUE)) 
m <- match(trg, src)

# rename files in tempdir 
file.rename(names(trg[!is.na(m)]), file.path(tempdir(), names(src[m[!is.na(m)]])))

# open temp dir in windows to check, if file names correspond to file contents (= it worked)
shell.exec(tempdir()) 
lukeA
  • 53,097
  • 5
  • 97
  • 100