R重命名文件扩展名

时间:2020-02-15 16:31:45

标签: r replace file-extension

我尝试查看File extension renaming in R并使用脚本没有任何运气。我的问题几乎相同。

我有一堆文件,文件扩展名为我要更改的文件。我使用了以下代码,但无法完成最后一步。

我知道之前也曾问过类似的问题,但是我只是被困住了,因此还是伸出援手。

startingDir<-"/Users/anders/Documents/Juni 2019/DATA"
endDir<-"/Users/anders/Documents/Juni 2019/DATA/formatted"

#List over files in startingDir with the extension .zipwblibcurl that I want to replace
old_files<-list.files(startingDir,pattern = "\\.zipwblibcurl")

#View(old_files)

#Renaming the file extension and making a new list i R changing the file extension from .zipwblibcurl to .zip
new_files <- gsub(".zipwblibcurl", ".zip", old_files)

#View(new_files)

#Replacing the old files in the startingDir. Eventually I would like to move them to newDir. For simplicity I have just tried as in the other post without any luck:...
file.rename( old_files, new_files)

运行file.rename后,我得到每个条目的输出FALSE。

2 个答案:

答案 0 :(得分:1)

完整的答案,包括@StephaneLaurent的评论:确保full.names = TRUE中有list.files();否则,将不会捕获文件的路径,而只会捕获文件名。

完整的代码段:

old = list.files(startingDir,
                 pattern = "\\.zipwblibcurl",
                 full.names = TRUE) #

# replace the file names
new <- gsub(".zipwblibcurl", ".zip", old )

# Rename old files names to the new file names
file.rename(old, new)

答案 1 :(得分:0)

就像@StéphaneLaurent所说,R很可能在当前工作目录中查找文件而找不到它们。您可以通过添加

来更正此问题

import .... class App extends React.Component{ render(){ return ( <View style={styles.container}> <Navigation /> <StackNavigation /> </View> ); } };