Home > Net >  Rename multiple csv files with value from inside file
Rename multiple csv files with value from inside file

Time:06-03

I have hundreds of csv files which are named like below

0000.csv
0001.csv
0002.csv
.....
.....

I would like to rename them with the first value from the second row (country value) in the file with an _increasing_number because multiple files would have the same value in the second row e.g. Singapore

Using a R script to do this but not able to achieve it yet.

fls <- list.files(path="./Users/.../", pattern = '\\.csv')

for(f in fls){
  x <- scan(file = f, what = character(), nmax = 2, nlines = 1, sep = ',')
  g <- paste0(x[2], '.csv')
  file.rename(f, g)
}

How can I modify this further to rename all files with country value & increasing file number?

CodePudding user response:

Assuming your path is correct, and fls contains a vector of file names, and the country value is indeed the second data item on the first row (you indicate second row, but you have nlines=1, restricting the file scan to the first row only), then you can do something like this:

path = "./Users/.../"
fls <- list.files(path=path, pattern = '\\.csv')

for(f in fls){
  x <- scan(file = paste0(path,f), what = character(), nmax = 2, nlines = 1, sep = ',')
  g <- paste0(x[2],"_",gsub("[^0-9]*","",f), '.csv')
  file.rename(paste0(path,f),paste0(path,g))
}

CodePudding user response:

Posting alternative solution. Someone helped me write a py script for this use case which worked really well to get the country values from the files & rename the file

import os
​
files = os.listdir("./")
for file in files:
    if file.endswith(".csv"):
        with open(file, 'r') as openfile:
            counter = 0
            for line in openfile:
                if counter == 0:
                    counter  = 1
                else:    
                    values = line.split(',')
                    if counter == 1:
                        rename = values[0]
                    else:
                        temp_rename = values[0]
                        if temp_rename in rename:
                            continue
                        else:
                            rename = rename   "_"   temp_rename
                    counter  = 1
        part1, part2 = file.split(".")
        newName = part1 "_" rename "." part2
        print(newName)
        os.rename(file, newName)
  • Related