计算.csv文件的数量

时间:2017-05-17 14:07:05

标签: r

我是R的新手,我正在尝试计算文件夹中的.csv文件数量。我创建了以下代码:

DATA <- "C:/Users/Stacey/Documents/R/Proj/RawDatabase/"

countFilesInDirectory <- function(strPathToDirectory, arrExtensions) {
  intFileCount = 0;
  arrFiles = scandir(strPathToDirectory);
  foreach(arrFiles as strFile) {

    if(is_dir("strPathToDirectory/strFile") && is_readable("strPathToDirectory/strFile")) {
      intFileCount += countFilesInDirectory("strPathToDirectory/strFile", arrExtensions);
    } 

  }

  return intFileCount;
}


intFileCount = countFilesInDirectory(DATA, array('csv'));
echo intFileCount

但我得到以下错误输出:

> DATA <- "C:/Users/Stacey/Documents/R/Proj/RawDatabase/"
> 
> countFilesInDirectory <- function(strPathToDirectory, arrExtensions) {
+   intFileCount = 0;
+   arrFiles = scandir(strPathToDirectory);
+   foreach(arrFiles as strFile) {
Error: unexpected symbol in:
"  arrFiles = scandir(strPathToDirectory);
  foreach(arrFiles as"
>     
>     if(is_dir("strPathToDirectory/strFile") && is_readable("strPathToDirectory/strFile")) {
+       intFileCount += countFilesInDirectory("strPathToDirectory/strFile", arrExtensions);
Error: unexpected '=' in:
"    if(is_dir("strPathToDirectory/strFile") && is_readable("strPathToDirectory/strFile")) {
      intFileCount +="
>     } 
Error: unexpected '}' in "    }"
>     
>   }
Error: unexpected '}' in "  }"
>   
>   return intFileCount;
Error: unexpected symbol in "  return intFileCount"
> }
Error: unexpected '}' in "}"
> 
> 
> intFileCount = countFilesInDirectory(DATA, array('csv'));
Error: could not find function "countFilesInDirectory"
> echo intFileCount
Error: unexpected symbol in "echo intFileCount"

1 个答案:

答案 0 :(得分:0)

给这一点:length(list.files("path/to/the/folder",pattern="*.csv"))

ps:每个EOL都不需要分号