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
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")
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.
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.
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")
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.
Related
I'm looking to copy a set of files from multiple directories into one new folder using this code:
file.copy(from = paste0('directory1', all_files),
to = paste0('directory2', all_files))
all_files is a character vector of files including their full path location.
I've also tried using basename(all_files) with no luck.
I've tried using the code above
Just following up - I've figured it out.
It wasn't working because the paste was redundant, this code fixed it:
for (n in 1:length(all_files)) {
file.copy(from = all_files[n],
to = 'directory2')
}
I think you need to use the below code, in the to argument we need to use the new folder path where the files need to the copied over
file.copy(from = paste0('directory1', all_files),
to = directory2)
I load my files (.RData) from a particular folder, and i created a subfolder to save some samples and subsets. So, i want to save these elements in the subfolder, and they don't have the same name structure because i have multiple datasets (for example it cannot be sub1, sub2 etc, i have to write try1, full_sample, sub_2021 and so on).
I tried the following :
subsets_samples <- file.path <-("/Volumes/WD_BLACK/Merge/SAMPLES_SUBSETS")
fname <- file.path(subsets_samples, ".RData")
save(mydata, file=fname)
But obviously there is a problem with the saving part. My goal is to have something like :
save(mydata, file = "newname")
With the .RData format from fname that is put automatically.
I saw some answers with loops and so on but i don't really understand the process i'm sorry.
Thanks !
The problem with file.path is that it will place a separator (e.g., /ΒΈ) between each of the elements. So you would have to use paste0 in addition for the actual file name:
# If I understand you correctly, you want the iteration, like try1, full_sample, sub_2021 and so on in your file name. define them somewhere in your loop/script
iteration <- "full_sample"
fname <- file.path("Volumes", "WD_BLACK", "Merge", "SAMPLES_SUBSETS", paste0(iteration, ".Rds"))
Additionally, I would suggest to use saveRDS instead of save, since it is the appropriate function if you want to save just one object.
saveRDS(mydata, file = fname)
I want to read files with extension .output with the function read.table.
I used pattern=".output" but its'not correct.
Any suggestions?
As an example, heres how you could read in files with the extension ".output" and create a list of tables
list.filenames <- list.files(pattern="\\.output$")
trialsdata <- lapply(list.filenames,read.table,sep="\t")
or if you just want to read them one at a time manually just include the extention in the filename argument.
read.table("ACF.output",sep=...)
So finally because i didn't found a solution(something is going wrong with my path) i made a text file including all the .output files with ls *.output > data.txt.
After that using :
files = read.table("./data.txt")
i am making a data.frame including all my files and using
files[] <- lapply(files, as.character)
Finally with test = read.table(files[i,],header=F,row.names=1)
we could read every file which is stored in i (i = no of line).
I am remming some files. First I create a list.
files_list<-list.files()
Now I edit the list of files so I can manually arrange them in any order I want.
new_files_list<-edit(files_list)
Using the file.rename function I am able to rename the files numerically.
How can I modify the command below so it uses the file order I manually set in the above command?
file.rename(from = list.files(pattern="file*"), paste0(stem_file_name,numeric_renaming, ".abc"))
this will rename all files in the folder by numerate them.
Be carfull with this, you can produce a lot of disorder in your system when you don't change the working directory with setwd().
setwd("/home/spazieren/playground")
files_list<-list.files()
new_files_list <- edit(files_list)
file.rename(from = files_list, to=new_files_list)
I am trying to loop through many folders in a directory, looking for a particular xml file buried in one of the folders. I would then like to save the location of that file and then run my code against that file (I will not include that code in this). What I am asking here is to loop through all the folders and then open the specific file.
For example:
My main folder would be: C:\Parsing
It has two folders named "folder1" and "folder2".
each folder has an xml file that I am interested in, lets say its called "needed.xml"
I would like to have a scrip that loops through the directory and finds those particular scripts.
Do you know how I could that in R.
Using list.files and greplyou could look recursively through all sub-folders
rootPath="C:\Parsing"
listFiles=list.files(rootPath,recursive=TRUE)
searchFileName="needed.xml"
presentFile=grepl(searchFileName,listFiles)
if(nchar(presentFile)) cat("File",searchFileName,"is present at", presentFile,"\n")
Is this what you're looking for?
require(XML)
fol <- list.files("C:/Parsing")
for (i in fol){
dir <- paste("C:/Parsing" , i, "/needed.xml", sep = "")
if(file.exists(dir) == T){
needed <- xmlToList(dir)
}
}
This will locate your xml file and read it into R as a list. I wasn't clear from your question if you wanted the output to be the data itself or just the directory location of your data which could then be supplied to another function/script. If you just want the location, remove the 'xmlToList' function.
I would do something like this (replace *.xml with your filename.xml if you want):
list.files(path = "C:\Parsing", pattern = "*.xml", recursive = TRUE, full.names = TRUE)
This will recursively look for files with extension .xml in the path C:\Parsing and return the full path of the matched files.