I haven't tracked what went before, but your syntax here is totally messed up:
for (i in seq_len(nrow(total_name))) { with( total_name[i, ], { file.rename(total_name$orig, total_name$target) } ) } Also, you don't need looping because file.rename is already vectorized -- you need to read the Help file more carefully. So something like this should do what you want: with(total_name, file.rename (orig, target)) where orig and target should be character (not factor) columns in your data frame. Cheers, Bert On Mon, Sep 19, 2022 at 10:38 AM Kai Yang via R-help <r-help@r-project.org> wrote: > Hi Rui,I put original file names and target file names into data frame: > total_name. I can use > file.rename("out_1.pdf", "abc_title.pdf") > > to rename a single file. But I hope to use loop to complete the task. So I > write a code below: > for (i in seq_len(nrow(total_name))) > { > with( > total_name[i, ], > { > file.rename(total_name$orig, total_name$target) > } > ) > } > But I got error message: > Error in file.rename(total_name$orig, total_name$target) : > invalid 'from' argument > > I think the problem happen due to quotation problem. Do you know how to > fix it? > Thanks, > Kai On Friday, September 16, 2022 at 08:32:53 PM PDT, Rui Barradas < > ruipbarra...@sapo.pt> wrote: > > Hello, > > My understanding of the problem is different, the files' first row is > not tabular data, I might be wrong but it seems to me that it's > something like > > first row [of] file1.txt [is]: > abc.txt > > file2.txt: > bed.txt > > etc. > > That's why the sapply loop reads one datum only and exits. > > Hope this helps, > > Rui Barradas > > Às 02:29 de 17/09/2022, Ebert,Timothy Aaron escreveu: > > The syntax might not be quite right, but why not something like > file.rename(colnames(dataframe)[1]) -- Using colnames to get the names of > the columns that are in the first row, selecting the first element from > colnames() and setting the file name equal to that. > > > > Do a for loop using current file names in some folder, and save to a new > folder. > > > > Tim > > > > -----Original Message----- > > From: R-help <r-help-boun...@r-project.org> On Behalf Of Kai Yang via > R-help > > Sent: Friday, September 16, 2022 1:52 PM > > To: R-help Mailing List <r-help@r-project.org>; Rui Barradas < > ruipbarra...@sapo.pt> > > Subject: Re: [R] rename files in R > > > > [External Email] > > > > 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat > >> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl > >> .edu%7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e > >> 1b84%7C0%7C0%7C637989475519163868%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w > >> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C > >> &sdata=lPW2XrQntw3V7eJmxkaoOXK75%2FO1hWgRv7FO3%2B5O3OQ%3D&rese > >> rved=0 PLEASE do read the posting guide > >> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r > >> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu% > >> 7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e1b84% > >> 7C0%7C0%7C637989475519320101%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM > >> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C& > >> sdata=%2FaSxvb47qFp7Mo6MFlCEQcvinHvV%2FWA%2Brs3keoNuTqk%3D&reserve > >> d=0 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://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637989475519320101%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=tCreTbW2QEMePdooZNVmtpl6kKSVMXv9E0oIBYBTMBQ%3D&reserved=0 > > PLEASE do read the posting guide > https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637989475519320101%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FaSxvb47qFp7Mo6MFlCEQcvinHvV%2FWA%2Brs3keoNuTqk%3D&reserved=0 > > 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. > [[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.