Thank you. I'll try this. --- Kai On Friday, September 16, 2022 at 11:01:33 AM PDT, Rui Barradas <ruipbarra...@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 > <ruipbarra...@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@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.