[R] rename files in R

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Sat Sep 17 02:06:40 CEST 2022


Just a suggestion to add to blindly renaming a file.

The request was for opening a file and reading something on the first line that is used to construct a filename.

Fine but make a checklist including items like this that may apply to your need.

- CLOSE any open file before renaming it.
- Make sure the name is UNIQUE so you do not over-write an existing file or fail because the new name is in use.
- Make sure the filename chosen  is valid on your target device and usable. That may mean avoiding some characters or suppressing spaces or replacing them with an underscore, making sure it does not start with a digit if that matters, and so on. And make sure you have a period and extension characters like .txt if that is needed and any number of other such considerations.
- Until you are SURE consider making a backup copy of the files in some way rather than just blindly renaming them.

If you encounter issues, decide on a secondary strategy. For example, if the header line on two files is identical, thus by the time you get to the second, file XYY.txt already appears, what do you do? Do you leave file69 alone or maybe name it XYZ2.txt or some other scheme of your choice? Note that this can get complex if you have dozens of files trying to be named the same.

Only YOU know what your files and data are like and what results you want. People here can only answer your question on the mechanics of how to do the one thing you asked for: renaming. Even then, please look carefully as the example blow accidentally asks you to "ename(...)" and my guess is that won't do what you want.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Rui Barradas
Sent: Friday, September 16, 2022 6:35 PM
To: Kai Yang <yangkai9999 using yahoo.com>; R-help Mailing List <r-help using r-project.org>
Subject: Re: [R] rename files in R

Hello,

Sorry, it should be


if(length(filenames) == length(destnames)) {
   file.ename(filenames, destname)
} else message("something went wrong")


Hope this helps,

Rui Barradas

Às 19:24 de 16/09/2022, Kai Yang escreveu:
>   Thank you. I'll try this. --- Kai
>      On Friday, September 16, 2022 at 11:01:33 AM PDT, Rui Barradas <ruipbarradas using sapo.pt> wrote:
>   
>   Hello,
> 
> Something like the following might work.
> 
> 
> 
> filenames <- list.files(pattern = "^file\\d+\\.txt$") destnames <- 
> sapply(filenames, scan, what = character(), sep = "\n", n = 1L, 
> strip.white = TRUE)
> 
> if(length(filenames) == length(destnames))
>    file.rename(filenames, destnames)
> else message("something went wrong")
> 
> 
> 
> But I would make copies of 2 or 3 files first and test with, say,
> 
> filenames[1:2] and destnames[1:2]
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 18:51 de 16/09/2022, Kai Yang escreveu:
>>    Hello,Here is the example:
>>      file name    first row  file1.txt     abc.txt  file2.txt     
>> bed.txt  file3.txt     gogo.txt  . . file1243.txt    last.txt I want to use loop because I need to read the first row information for first file, and then rename the file, then go to next file. I'm not sure if this is right way to approach my goal. Any suggestion will be appreciated. Thanks, Kai    On Friday, September 16, 2022 at 10:38:32 AM PDT, Rui Barradas <ruipbarradas using sapo.pt> wrote:
>>    
>>    Hello,
>>
>> Please post the first row of 2 or 3 files and the expected result.
>>
>> You can rename files with ?file.rename. This function is vectorized 
>> its on arguments so you do not need a loop, only the source and 
>> destination filenames. Both vectors should have the same length, if 
>> not strange things will occur including data loss.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 18:26 de 16/09/2022, Kai Yang via R-help escreveu:
>>> Hello,I have a lot of files with not meaningful name, such as:  
>>> file1.txt, file2.txt ...... I need to rename them using the 
>>> information from the first row of the files. Now I can get the 
>>> information from the first row of each file. Now, I need know how to 
>>> rename them in R (using loop?). Thank you for your helpKai
>>>
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>      
>

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list