Question: (Closed) Rename Files In Subfolders In R
1
gravatar for Jason
7.1 years ago by
Jason20
Jason20 wrote:

I have a folder "Samples" with sub-folders named "A01" to "A11". Each folder contains 2 files i.e. probes.txt and snps.txt. I want to write a code that reads the subfolders names, go into each subfolder and rename the 2 text files with the subfolder name in it e.g. A01_probes.txt

I wrote this code and R and I can't work out what's the problem. For me, the length of both "from" and "to" are same!

Help!


for (i in 1:length(b))
{
  setwd(b[i])
  vr1<-cat(basename(b[i]),"pro.txt",sep="")
  vr2<-cat(basename(b[i]),"s.txt",sep="")
  file.rename("probes.txt",paste(vr1))
  file.rename("snps.txt",paste(vr2))
}

Error in file.rename("probes.txt", paste(vr1)) : 'from' and 'to' are of different lengths

R • 6.1k views
ADD COMMENTlink modified 7.1 years ago • written 7.1 years ago by Jason20
1

Try changing cat with paste. Please edit you code, it is hard to read.

ADD REPLYlink modified 7.1 years ago • written 7.1 years ago by zx87549.7k

Thanks a bunch! it worked!

ADD REPLYlink written 7.1 years ago by Jason20
1

Can someone put the code in a code block so it renders properly?

As zx8754 alluded, cat doesn't return a value.

ADD REPLYlink modified 7.1 years ago • written 7.1 years ago by Devon Ryan97k
1

Sorry about that. I'm new here. Just fixed it.

ADD REPLYlink written 7.1 years ago by Jason20
1

Well, not a bionformatic question, and it's already solved, closing.

ADD REPLYlink written 7.1 years ago by JC11k

Sorry, the code was required to prepare input files for ASCAT (Allele specific copy number analysis in Tumors) algorithm for a particular array type. Will mention "bioinformatic" background next time if you "really" need it.

ADD REPLYlink written 7.0 years ago by Jason20

The forum requires any post to be bioinformatic-related, otherwise you will be directed to stackoverflow or similar sites. This is a rule #1 for Editors, not only me. Cheers.

ADD REPLYlink written 7.0 years ago by JC11k

Always when you used code like this you probably have a problems so I highly recomend Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org

ADD REPLYlink modified 3.0 years ago • written 3.1 years ago by gradimir_sancanin0
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1098 users visited in the last hour