Moving files between folders
Asked Answered
U

6

66

I want to copy/paste a file from one folder to another folder in windows using R, but it's not working. My code:

> file.rename(from="C:/Users/msc2/Desktop/rabata.txt",to="C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP/rabata.tx")

[1] FALSE
Unfortunate answered 22/4, 2012 at 9:55 Comment(7)
Does the BADMASHI/SCOP folder already exist?Walsh
There's nothing wrong with your syntax; make sure the source file and dest folder already exist.Tarim
did you try file.copy instead of file.rename?Modernize
In addition to the other comments, please state the exact error you got.Sass
What does file.exists("C:/Users/msc2/Desktop/rabata.txt") tell you?Quanta
> file.exists("C:/Users/msc2/Desktop/rabata.txt") [1] TRUEUnfortunate
> file.exists("C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP") [1] TRUE <br/>but > file.exists("C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP/") [1] FALSEUnfortunate
C
76

If you wanted a file.rename()-like function that would also create any directories needed to carry out the rename, you could try something like this:

my.file.rename <- function(from, to) {
    todir <- dirname(to)
    if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
    file.rename(from = from,  to = to)
}

my.file.rename(from = "C:/Users/msc2/Desktop/rabata.txt",
               to = "C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP/rabata.txt")
Culbert answered 22/4, 2012 at 13:18 Comment(2)
I am trying to move a file from /tmp to ~, but I am getting a 'Invalid cross-device link', how could I solve this?Sinistrality
file.rename cannot move files between different mount points. I guess that in your case /tmp and /home are on separate partitions and mount points. In such a case you can either do a copy and then remove the old file, or you can use file_move from the fs package, which seems to be able to move files across mount points.Comedian
U
41

Please just be aware that file.rename will actually delete the file from the "from" folder. If you want to just make a duplicate copy and leave the original in its place, use file.copy instead.

Upsetting answered 13/2, 2014 at 23:6 Comment(1)
Is file.rename written such that if the process is interrupted, the file won't be lost or corrupted?Councilor
T
17

Use file.copy() or fs::file_copy()

file.copy(from = "path_to_original_file",
          to   = "path_to_move_to")

Then you can remove the original file with file.remove():

file.remove("path_to_original_file")

Update 2021-10-08: you can also use fs::file_copy(). I like {fs} for consistent file and directory management from within R.

Tawanda answered 13/9, 2019 at 18:43 Comment(0)
O
5

You can try the filesstrings library. This option will move the file into a directory. Example code:

First, we create a sample directory and file:

dir.create("My_directory")
file.create("My_file.txt")

Second, we can move My_file.txt into the created directory My_directory:

file.move("My_file.txt", "My_directory")
Omalley answered 23/5, 2018 at 21:58 Comment(0)
Y
1
fs::file_move("oldfile.txt", "some/other/existing/directory/newfile.txt")
Yance answered 10/10, 2023 at 10:37 Comment(0)
B
0

You are missing a "t" letter in the second extension. Try this:

file.rename(from="C:/Users/msc2/Desktop/rabata.txt",to="C:/Users/msc2/Desktop/Halwa/BADMASHI/SCOP/rabata.txt").

Additionally, it could be worth it to try the file.copy() function. It is specifically designed to copy files instead of renaming.

Biomedicine answered 5/11, 2020 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.