处理临时文件路径

Manipulating temporary file paths

我在 tempdir() 中的不同临时路径上有一个文件列表。路径在此处给出,其中 /tmp/Rtmp9pF0OF 是我的 tempdir() 用于我的 R 会话。

[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" 

这里给出了我本地目录中的文件名

 "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"

我想通过用上面向量中给出的名称更改文件名来操纵 tempdir() 中的文件路径。说这样的话。

[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" 

谢谢。

这是一种通过查看 MD5 哈希将一个目录中的文件名与临时目录中的文件名进行匹配的方法:

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