【问题标题】:Moving large amounts of files from one large folder to several smaller folders using R使用 R 将大量文件从一个大文件夹移动到几个较小的文件夹
【发布时间】:2021-04-22 04:21:31
【问题描述】:

我在一个文件夹中有超过 7,000 个 .wav 文件,需要将它们分成 12 个一组并放入单独的较小文件夹中。

文件对应每 5 分钟录制 1 分钟的记录,因此每 12 个文件对应 1 小时。

文件存储在我电脑的工作目录中:“E:/Audiomoth Files/Winter/Rural/Emma/”

文件名示例如下:

20210111_000000.wav
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

那就是一小时

20210111_010000.wav
20210111_010500.wav
20210111_011000.wav

等等。

我需要将文件分成 12 个一组,然后我需要在以下位置创建一个新文件夹:“E:/Audiomoth Files/Winter/Rural/Emma/Organised Files”

使用名为“Hour 1”、“Hour 2”等的新文件夹。

我需要执行此操作的确切代码是什么?

很明显,我是 R 的初学者,所以如果可以用通俗易懂的方式来说明答案,那就太好了。

提前谢谢你

【问题讨论】:

  • 这个问题没有回答吗?
  • 我运行了您在上一个问题上给出的答案的代码,但似乎没有发生任何事情
  • R 中似乎没有发生任何事情,但是在您的工作目录中查找文件夹,您应该会看到其中的文件按小时分开。也没有人会给你确切的代码来解决你的问题,我们给出近似值然后你应该改变它以适应你遇到的问题
  • 我认为工作目录文件夹没有任何变化。它仍然只是一个包含数千个文件的巨大文件夹。很抱歉要求确切的东西,只是如果某些东西不起作用,我对自己更改它以使其起作用非常不确定和不自信。就像我说的那样,我真的是第一次学习这个。

标签: r file


【解决方案1】:

这样的?

为了防止数据丢失,我故意使用复制而不是剪切。我编辑了答案,因此文件将保留其旧名称。我命令他们给他们新的名字,例如,将最后一行的name 替换为"Part_", i, ".wav"

# get a list of the paths to all the files
old_files <- list.files("E:/Audiomoth Files/Winter/Rural/Emma/", pattern = "\\.wav$", full.names = TRUE)
# create new directory
dir.create("E:/Audiomoth Files/Winter/Rural/Emma/Organised Files")

# start a loop, repeat as often as there are groups of 12 within the list of files
for(i in 1:(round(length(old_files)/12)+1)){
  # create a directory for the hour
  directory <- paste("E:/Audiomoth Files/Winter/Rural/Emma/Organised Files", "/Hour_", i, sep = "")
  dir.create(directory)
  # select the files that are to copy (I guess it will start with 1*12-11 = 1st file
  # and end with i*12 = 12th file)
  filesToCopy <- old_files[(i*12-11):(i*12)]
  # for those files run another loop:
  for(file in 1:12){
  # get the name of the file
  name <- basename(filesToCopy[file])
  # copy the file to the current directory
   file.copy(filesToCopy[file], paste(directory, "/", name, sep = ""))
  }
}

如果您不完全确定,我建议您复制文件而不是直接移动它们(我希望这里的脚本能做到这一点)。您可以稍后手动删除它们。 您检查了一切正常并且所有数据都在应有的位置之后。否则即使是很小的错误也可能导致数据丢失,这是我们不希望发生的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多