Home > Enterprise >  How to rename local files on machine in R?
How to rename local files on machine in R?

Time:07-29

I am trying to rename 90 PDF files on my machine to a specific format.

My current files are named as such:

file_name 
(P102180.R2858.M60102148)SupplierPerformanceDashboard.PDF
(P10424.R2858.M60010424)SupplierPerformanceDashboard.PDF
(P14479.R2858.M60004820)SupplierPerformanceDashboard.PDF
(P14479.R2858.M60031167)SupplierPerformanceDashboard.PDF
(P14479.R2858.M60032342)SupplierPerformanceDashboard.PDF

I was able to extract a column I need to rename the files as such:

 file_name                                                            MVNDR_NBR
(P102180.R2858.M60102148)SupplierPerformanceDashboard.PDF               60102148
(P10424.R2858.M60010424)SupplierPerformanceDashboard.PDF                60010424
(P14479.R2858.M60004820)SupplierPerformanceDashboard.PDF                60004820
(P14479.R2858.M60031167)SupplierPerformanceDashboard.PDF                60031167
(P14479.R2858.M60032342)SupplierPerformanceDashboard.PDF                60032342

I then did a join and concatenation and now my table looks as such enter image description here

How can I make new_file_name replace the old file_names locally on my machine

CodePudding user response:

We may use file.rename (assuming these files are in the working directory)

file.rename(df1$file_name, df1$new_file_name)

CodePudding user response:

First,you should make sure the current working directory is the path your PDF files exists.(You can use 'dir()' or 'list.files()' to check).Then you can rename these files using the script:

    files <- list.files()
    n <- 1
    for (f in files)
    {
       newname <- df$new_file_name[n]
       file.rename(f,newname)
       n =1           
    }

Be carefule the newname is corresponding to the original file name!

  • Related