简体   繁体   中英

How to use R to batch rename many files based on sequential parent folder names

I have 1 TB of video and associated files for my MSc project, and I am trying to rename all the files in R. The parent folder-subfolder layout for each video camera is as such:

  • Camera ID (eg, C00125)
    • YYYY_MM
      • DD
        • HH (in 24-hour format)
          • CameraID_HHMMSS_#15 (File name)
      • DD
      • DD
      • DD
    • YYYY_MM

Within each of the final subfolders, there are a variable number of files, and each unique file name has 3 different file types associated with it: an AVI File, LBC File, and Wav File. So, if a final subfolder only had 1 video (there can be up to 6 videos), there would still be 3 files, eg C00141_192000_#15, C00141_192000_#15.lbc, and C00141_192000_#15. The LBC Files have a file extension ".lbc" at the end of each file name.

I need to compile all the files from each camera into 1 folder per camera, but frustratingly I would then have a bunch of files with the same file name based on HHMMSS, and the file name would not indicate what YYYYMMDD each file is actually associated with. As such, I need to rename each file so that it has the corresponding YYYYMMDD added to the current filename which already has the CameraID and HHMMSS. The best way I can see to do this would be to take the YYYYMMDD information from the previous three parent folders of each file (unless you could somehow skip the HH subfolder). I don't care whether or not I keep the "_#15".

Ideally this would result in the file name C00141_2021_05_09_192000_#15 (for each of the three file types from the above example). But given the nature of the subfolders, I would be fine with something like 2021_05_09_19_C00141_192000_#15 . All that really matters is that the file name is unique to its corresponding CameraID, YYYYMMDD, and HHMMSS, and that the files can be sorted chronologically in file explorer once they have been renamed and pooled together.

I have looked into multiple other forums, but the answers are too specialized to the particular file names in the post and don't address multiple subfolders. I have also looked into the "Bulk Rename Utility" Web app, which does roughly what I need, but only 1 subfolder at time, which is not practical for the amount of subfolders I have (15 cameras x 3 months x ~30 days x 24 hours)

I have been struggling with this for 2 full days, and I haven't been able to make much progress. I can't make any progress on my project until I figure this out, so I would really appreciate any help. I am relatively new to R and programming.

Here's how I'd do it. This assumes your working directory is the folder with all the "Camera ID (eg, C00125)" files. Also, I HIGHLY recommend that you make a backup of your data before you do this. I'm using file.copy() here to be safe. file.rename() is probably faster...

You could easily use regex to rename the files to whatever you want based on their file path. If you are picky about this, let me know, but based on your question, this isn't a huge deal. I'm suggesting using the simplest way of making sure the videos names retain as much info as possible.

There is no guarantee that this will work. Experiment with a subset of your data first. Back up your data before attempting this!

# Create new folder to move all video files into
new_dir <- "./new_dir"
dir.create(new_dir)

# Get all target files
old_names <- list.files(recursive=TRUE)

# The new file name is just the old file path but instead of 
# slashes there are underscores
new_names <- gsub(pattern="/", replacement="_", x=old_names)

# Copy files
file.copy(from=old_names, to=paste0(new_dir, "/", new_names))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM