[英]Rename files in R
I have problem with renaming files from R
. 我在从
R
重命名文件时遇到问题。
In my folder on Desktop there are 10 files: 在桌面上的文件夹中有10个文件:
račun 1.xlsx
račun 2.xlsx
... ...
račun 10.xlsx
I have tried the following: 我尝试过以下方法:
files <- list.files(path = "myfolder")
file.rename(files,
paste0("novi_", 1:10, ".xlsx"))
This is what I get as an outcome: 这就是我得到的结果:
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
I suppose this is because of unicode character č, but I do not know how to find a solution for this. 我想这是因为unicode字符č,但我不知道如何为此找到解决方案。
EDIT 2: The solution here was for the OP to change the Region settings in Control Panel, setting format to be in Serbian(Latin, Serbia). 编辑2:这里的解决方案是OP改变控制面板中的区域设置,设置格式为塞尔维亚语(拉丁语,塞尔维亚语)。
EDIT 1: See the comments: the OP is on a Windows machine. 编辑1:请参阅注释:OP在Windows计算机上。 Here the problem is that
list.files()
(and presumably dir()
, since they call the same .Internal) is converting the non ASCII filenames to ASCII, but Windows is expecting file.exists()
to send it the unicode filenames, (and presumably also file.rename()
) 这里的问题是
list.files()
(可能是dir()
,因为它们调用相同的.Internal)将非ASCII文件名转换为ASCII,但是Windows希望file.exists()
向它发送unicode文件名, (也可能是file.rename()
)
Try: 尝试:
file.rename(gsub("c", "č", files), paste0("novi_", seq_along(files, ".xlsx"))
# could work, but it didn't for `file.exists()`
Original answer: 原始答案:
setwd(<your path>)
(files <- list.files())
# [1] "račun 1.xlsx" "račun 2.xlsx" "račun 3.xlsx" "račun 4.xlsx" "račun 5.xlsx [6] "račun 6.xlsx"
file.rename(files, paste0("novi_", seq_along(files, ".xlsx"))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE
The fact that you specified a path in list.files()
, suggests that you're not in the correct directory 您在
list.files()
指定路径的事实表明您不在正确的目录中
One way to get around this is to use the 8.3 version of a filename , which is guaranteed to be ASCII-only. 解决此问题的一种方法是使用8.3版本的文件名 ,该文件名保证仅限ASCII。 The main problem is that (as far as I know) there's no way to get this programmatically in R, so you should double-check that this is correct:
主要问题是(据我所知),无法以编程方式在R中进行此操作,因此您应该仔细检查这是否正确:
files <- paste0("RAUN~", 1:10, ".XLS")
newfiles <- paste0("novi_", 1:10, ".xlsx")
file.rename(files, newfiles)
You can get the 8.3 filenames with DIR /X
from the commandline. 您可以
DIR /X
获取带有DIR /X
的8.3文件名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.