如何在多个文件或目录中的所有文件上运行相同的代码 [英] How to run same code on multiple files, or all files in directory

查看:110
本文介绍了如何在多个文件或目录中的所有文件上运行相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对编码非常陌生,最近写了一个涉及R和sox的小程序.看起来像这样

so I am very new to coding and recently wrote a little program that involved R and sox. It looked like this

file <- "test.mp3"

testSox = paste("sox ",file," -n spectrogram -o ",file,".png stats",sep='')
sox = system(testSox, intern = TRUE)
print(sox)

现在,我不想在代码中手动分配一个文件,而是想让此代码自动读取文件夹中的所有mp3.这可能吗?任何帮助将不胜感激.谢谢!

Now, instead of assigning the one file manually within the code, I would just like to have this code read through all the mp3s in a folder automatically. Is this possible? Any help would be greatly appreciated. Thanks!

实际上,我应该补充一点,我尝试了list.files,但是在运行system()命令时,我得到了

Actually, I should add that I tried list.files, but when it comes to running the system() command, I get

系统错误(命令(如as.integer(flag),f,stdout,stderr):字符串应作为第一个参数"

"Error in system(command, as.integer(flag), f, stdout, stderr) : character string expected as first argument"

这是我尝试过的list.files代码:

Here's the list.files code I tried:

> temp = list.files(path = ".", pattern=".mp3")
> 
> file <- temp
> 
> firstSox = paste("sox ",file," -n spectrogram -o ",file,".png stats",sep='')
> sox = system(firstSox, intern = TRUE)
Error in system(command, as.integer(flag), f, stdout, stderr) : 
  character string expected as first argument
> print(sox)

我猜这不是正确的路线吗?因为我基本上需要用temp数组中的每个mp3替换firstSox行中的文件".所以不要运行:

I'm guessing this is not the correct route to go? Because I basically need to replace 'file' in the firstSox line with each mp3 that's in the temp array. So instead of running:

file <- "test.mp3"

...我想让它为文件夹中的每个文件每次都重新分配... ,因此它以test.mp3,然后是1.mp3的方式运行2.mp3,然后是3.mp,等等.

...I would just like to have it re-assign each time for every file in the folder..., so it runs through as test.mp3, then 1.mp3, then 2.mp3, then 3.mp, etc.

我已经搜寻过网,就像感觉自己撞到了砖墙一样.如评论中所述,我已经阅读了循环,但是由于某些原因,我无法将精力集中在如何将其结合到我编写的内容中.我觉得我只需要一个人至少向我展示一下方式,或者甚至给我写一个例子,这样我就可以全神贯注.非常感谢您的帮助以及关于我做错了什么并且可以纠正的任何提示.谢谢.

I've scoured the net, and just feel like I've hit a brick wall. As stated in the comments, I've read up on loops, but for some reason I can't wrap my head around how to incorporate it into what I have written. I feel like I just need someone to show me at least the way, or maybe even write me an example so I can wrap my head around it. Would greatly appreciate help and any tips on what I'm doing wrong and could correct. Thanks.

推荐答案

尝试以下代码.我使用dir()而不是list.files,只是因为我发现它更容易.请记住,在R中有很多方法可以执行相同的操作.

Try the below code. I am using dir() instead of list.files, just because I find it easier. Remember there are many ways to do the same thing in R.

files <- dir(path = ".",pattern = ".mp3") #Get all the mp3 files

for(f in files) {     #Loop over the mp3 files one at a time

    firstSox = paste("sox ",f," -n spectrogram -o ",f,".png stats",sep='')
    sox = system(firstSox, intern = TRUE)
    print(sox)

}

这篇关于如何在多个文件或目录中的所有文件上运行相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆