1

Apologies for the simplicity of the question as I am a novice with R.

I have a large number of 1-minute audio files, with 1 minute recorded every 5 minutes. I need them organised by hour and saved to a new folder, so every 12 files need to be saved to a new folder. I have 7472 of these files so doing this manually would take too long.

Here is an example of the file names:

20210111_000500.wav,
20210111_001000.wav,
20210111_001500.wav,
20210111_002000.wav,
20210111_002500.wav,
20210111_003000.wav,
20210111_003500.wav,
20210111_004000.wav,
20210111_004500.wav,
20210111_005000.wav,
20210111_005500.wav,

which I want to all be in one folder, with the next hour starting as 20210111_010000.wav and so on.

How would I go about doing this?

Any help is much appreciated, thank you!

HarHar
  • 35
  • 5
  • You need to group them then copy them into the directory that you want – Onyambu Apr 21 '21 at 15:33
  • How would I go about doing this? What would the code be? Sorry I don't know what I'm doing. – HarHar Apr 21 '21 at 15:41
  • You said the code did not do anything. was there a folder called `my_folder` which was created? – Onyambu Apr 21 '21 at 17:41
  • Also does `nms` contain the names of the files you want to move? – Onyambu Apr 21 '21 at 17:43
  • no it doesn't, should I have entered the file names into the code somewhere? – HarHar Apr 21 '21 at 17:46
  • No you should not. That means that the files are in a specific folder not in the working directory. try `nms <- list.files(path to your folder that contains the wav files, pattern = "\\.wav", full.names = TRUE)` – Onyambu Apr 21 '21 at 17:47
  • In the global environment, nms says character (empty), is this correct? – HarHar Apr 21 '21 at 17:58
  • No that is not correct. First where are the files stored? Now check the path to the folder in which the files are stored, and try running `list.files` to ensure that `nms` contains all the files that you need to rearrange. try `help(list.files)` in order to understand how to use the function – Onyambu Apr 21 '21 at 18:00
  • Okay, nms now contains all the elements. The issue was that the files are .WAV rather than .wav ... However, my new problem is that 'groups' is still a list of 0 – HarHar Apr 21 '21 at 18:20
  • what does `strptime(nms, "%Y%m%d_%H%M%S")` give you? – Onyambu Apr 21 '21 at 18:23
  • strptime(nms, "%Y%m%d_%H%M%S") just returns NA for every entry – HarHar Apr 21 '21 at 18:27
  • That is where the problem lies. Could you try `strptime(basename(nms), "%Y%m%d_%H%M%S")` – Onyambu Apr 21 '21 at 18:28
  • I did this and it grouped all the files from the first day into hours. However, I then ran the rest of the code and all the files have disappeared. Luckily I copied the files before I started so I still have them backed up. – HarHar Apr 21 '21 at 18:40
  • Its not that they have disappeared. Check the folder called `my_folder` within your directory. All the files should be in there. But with respect to their groupings – Onyambu Apr 21 '21 at 18:42
  • There is no folder called `my_folder` , could it be because there are so many files and it takes some time to process? Also do I need to alter the code to account for the different days reflected in the file names? – HarHar Apr 21 '21 at 18:45
  • I have editted the code. Try it out – Onyambu Apr 21 '21 at 18:51
  • This has taken the 12 files from the first hour of every day (total of 312 files) and put them together in a single folder named `hour_00` within `my_folder` . I also get the error message `Error in (function (x, y, folder = "my_folder") : fl is not TRUE In addition: Warning message: In dir.create(folder) : Error in (function (x, y, folder = "my_folder") : fl is not TRUE`. How do I adjust it to get a separate folder for each hour of each day? Thank you for all your help by the way and sorry for my ineptitude. – HarHar Apr 21 '21 at 19:13
  • well thats a step. So to get the files into each day, you will have to change the `groups` line have it as `groups <- split(nms, format(strptime(nms, "%Y%m%d_%H%M%S"),"%Y%m%d_hour_%H"))` instead – Onyambu Apr 21 '21 at 19:15
  • check the edits – Onyambu Apr 21 '21 at 19:17

2 Answers2

1

You could try something along these lines. I first find all the .wav files, then define the folders, create the folders and finally move the .wav files to the new folders.

library(tidyverse)

setwd("path_where_wav_files_are_located_goes_here")

# a vector of paths to all .wav files in the specified directory
original_paths <- dir(pattern=".WAV$", full.names = T) %>% str_replace("\\.", getwd())
# only the file names
file_names <- dir(pattern=".WAV$")

# creating a data.frame with original paths and new paths where files are to be moved
df <- tibble(original_paths, file_names) %>%
  mutate(
    folder_name = (row_number() %/% 12 + 1) %>% paste0("_", .), # using integer division so that i have a dedicated folder for each 12 files
    new_path = paste0(getwd(), "/", folder_name, "/", file_names)
  )

# creating the directories
df$folder_name %>%
  unique() %>%
  paste0(getwd(), "/",  .) %>%
  map(
    ~dir.create(.x)
  )

# moving the files
walk2(df$original_paths, df$new_path, ~file.rename(.x, .y))
Jakub.Novotny
  • 2,912
  • 2
  • 6
  • 21
  • Thank you for your help, however when creating the directory, I get the error message: `Warning message: In dir.create(.x) : 'E:\Audiomoth Files\Winter\Rural\Emma' already exists` , which is my folder with all of the files in it. What do I do here? – HarHar Apr 21 '21 at 16:45
  • @HarHar I have edited my answer so that it accounts for the ".WAV" suffix rather then ".wav". You should check if the vectors/df contain the intended data before proceeding with the code. – Jakub.Novotny Apr 21 '21 at 19:04
0

You could do something in the lines of:

nms <- list.files(pattern = "\\.wav", full.names = TRUE)
groups <- split(nms, format(strptime(nms, "%Y%m%d_%H%M%S"),"%Y%m%d_hour_%H"))

f <- function(x, y, folder = "my_folder"){
  if(!die.exists(folder)){
      fl <- dir.create(folder)
      stopifnot(fl)
   }
  new_dir <- file.path(folder, y)
  if(!dir.exists(new_dir)) dir.create(new_dir)
  file.copy(x, file.path(new_dir, basename(x)))
  file.remove(x)
}

Map(f, groups, names(groups))
Onyambu
  • 67,392
  • 3
  • 24
  • 53