7

There are some .fcs files in a data.000X format (where X = 1, 2, 3...) in a directory.

I want to rename every n file to the following format: exp.fcs (where exp is a text from a vector) if the file to be renamed is an .fcs file.

in other words: I want to rename files to exp.txt, where exp is a text and not a consecutive letter(s) i.e. F, cA, K, etc.

For example, from:

data.0001, data.0002, data.0003, data.0004, data.0005, data.0006...  

to

textF_a.fcs, textF_b.fcs, textF_c.fcs, textVv_a.fcs, textVv_b.fcs, textVv_c.fcs ...  

I tried to do it with file.rename(from, to) but failed as the arguments have different lengths (and I don't know what it means):

a <- list.files(path = ".", pattern = "data.*$")  
b <- paste("data", 1:1180, ".fcs", sep = "")  
file.rename(a, b)
zx8754
  • 52,746
  • 12
  • 114
  • 209
abc
  • 167
  • 1
  • 4
  • 18
  • FWIW, the `rename` command in Linux supports Perl-style regular expressions (it was authored by Larry Wall). Also, the code given would be safer as `seq(a)`. – Iterator Oct 23 '11 at 16:34

3 Answers3

4

Your code "works" on my machine ("works" in the sense that, when I created a set of files and followed your procedure, the renaming happened correctly). The error is likely that the number of files that you have (length(a)) is different from the number of new names that you give (length(b)). Post back if it turns out that these objects do have the same length.

Charlie
  • 2,801
  • 3
  • 26
  • 27
  • Thank you @Charlie :)! Unfortunately I faced some other problems: when i type `file.rename(a, b)` it returns many FALSEs and after a `library(flowCore')` the function `isFCSfile("data.001")` it returns FALSE again :(... What's wrong...? – abc Oct 23 '11 at 07:53
  • 1
    @stan: please open a new question regarding your comment. BTW if you want `b` to be the same length, try: `b <- paste("data", 1:length(a), ".fcs", sep = "")` – daroczig Oct 23 '11 at 08:16
  • @stan if this answer appears to be correct, consider accepting it (the check at the left should turn green when you click it). – Roman Luštrik Oct 23 '11 at 09:09
  • @stan try giving us a reproducible example. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Oct 23 '11 at 09:57
4

Based on your comments, one issue is that your first file isn't named "data.001" - it's named "data.1". Use this:

b <- sprintf("data%.4d.fcs", seq(a)) 

It prepends up to 3 0s (since it seems you have 1000+ files, this may be better) to indices < 1000, so that all names have the same width. If you really just want to see things like "data.001", then use %.3d in the command.

Iterator
  • 20,250
  • 12
  • 75
  • 111
  • I think your answer here is the solution to the question at http://stackoverflow.com/questions/7865177/problems-with-flowcore-package , not this one ... because it affects the **output** format, not the number of files detected – Ben Bolker Oct 23 '11 at 16:59
  • @BenBolker I suspect that the file in the other question doesn't exist, because he didn't format things in the way intended. As for the argument lengths, `seq(a)` should ensure that `b` is the same length as `a`, no? The OP split the question, but the other question really arises because his expectations for the renaming were possibly incorrect. For the other question, I asked him to produce `file.info()` output. If the file really does exist, it's not from the renaming done by the OP or the other answerer. :) – Iterator Oct 23 '11 at 18:07
  • @stan glad to help. Does it mean your other question no longer applies? – Iterator Oct 24 '11 at 11:32
  • @Iterator: yes, thanks to all who replied to these questions :) – abc Oct 24 '11 at 14:38
2

As with the (very similar) question here, this function might be of use to you. I wrote it to allow regex find and replace in R. If you're on a mac it can detect and use the frontmost Finder window as a target. Also supports test runs, over-write control, and filtering large folders.

umxRenameFile <- function(baseFolder = "Finder", findStr = NA, replaceStr = NA, listPattern = NA, test = T, overwrite = F) {
    # uppercase = u$1
    if(baseFolder == "Finder"){
        baseFolder = system(intern = T, "osascript -e 'tell application \"Finder\" to get the POSIX path of (target of front window as alias)'")
        message("Using front-most Finder window:", baseFolder)
    } else if(baseFolder == "") {
        baseFolder = paste(dirname(file.choose(new = FALSE)), "/", sep = "") ## choose a directory
        message("Using selected folder:", baseFolder)
    }
    if(is.na(listPattern)){
        listPattern = findStr
    }
    a = list.files(baseFolder, pattern = listPattern)
    message("found ", length(a), " possible files")
    changed = 0
    for (fn in a) {
        findB = grepl(pattern = findStr, fn) # returns 1 if found
        if(findB){
            fnew = gsub(findStr, replace = replaceStr, fn) # replace all instances
            if(test){
                message("would change ", fn, " to ", fnew)  
            } else {
                if((!overwrite) & file.exists(paste(baseFolder, fnew, sep = ""))){
                    message("renaming ", fn, "to", fnew, "failed as already exists. To overwrite set T")
                } else {
                    file.rename(paste(baseFolder, fn, sep = ""), paste(baseFolder, fnew, sep = ""))
                    changed = changed + 1;
                }
            }
        }else{
            if(test){
                # message(paste("bad file",fn))
            }
        }
    }
    message("changed ", changed)
}
Community
  • 1
  • 1
tim
  • 3,559
  • 1
  • 33
  • 46