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.
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())