【问题标题】:Arduino SD -> File extensionArduino SD -> 文件扩展名
【发布时间】:2014-01-15 20:45:45
【问题描述】:

我发现这个例子列出了 SD 卡上的所有文件:

void printDirectory(文件目录,int numTabs){ 而(真){

 File entry =  dir.openNextFile();
 if (! entry) {
   // no more files
   //Serial.println("**nomorefiles**");
   break;
 }
 for (uint8_t i=0; i<numTabs; i++) {
   Serial.print('\t');
 }
 if (entry.type
 Serial.print(entry.name());
 if (entry.isDirectory()) {
   Serial.println("/");
   printDirectory(entry, numTabs+1);
 } else {
   // files have sizes, directories do not
   Serial.print("\t\t");
   Serial.println(entry.size(), DEC);
 }

} }

但我只想列出具有显式扩展名的文件并将它们保存到数组中。你们中的任何一个想法?我找不到任何函数来获取 SD 类的文件扩展名。

【问题讨论】:

  • 显式扩展名是指具有您明确选择的扩展名的文件?
  • 我的意思是,例如。列出所有以 .jpg 作为扩展名的文件并将它们保存到一个数组中
  • 如果没有执行此操作的函数,您可以通过将else 子句中的entry.name() 拆分为. 来识别文件的扩展名,因为文件名通常跟随在后面格式为name.extension,除非它没有扩展名,在这种情况下它只是name
  • if (entry.type 应该被删除。

标签: c embedded arduino


【解决方案1】:

不幸的是,您需要单独遍历每个文件名。

请查看我的代码示例。类似于“一个人”的上述评论。

这里是我实际使用WHERE I use itTHE filter function的链接

请注意,我上面的使用是使用 SdFatLib,它是 SD(IDE 提供的)库的更高级版本。下面我为 SD 调整了相同的功能。应该可以工作,因为它仅检查指向的字符数组的最后 4 个字符。

仅供参考 - 请注意,SD 和 SdFatLib 仅支持 8.3 格式。

void printDirectory(File dir, int numTabs) {
  while(true) {
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i=0; i<numTabs; i++) {
      Serial.print('\t');
    }

    if ( isFnMusic(entry.name()) ) { // Here is the magic
      Serial.print(entry.name());
    }

    if (entry.isDirectory()) { // Dir's will print regardless, you may want to exclude these
      Serial.print(entry.name());
      Serial.println("/");
      printDirectory(entry, numTabs+1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

bool isFnMusic(char* filename) {
  int8_t len = strlen(filename);
  bool result;
  if (  strstr(strlwr(filename + (len - 4)), ".mp3")
     || strstr(strlwr(filename + (len - 4)), ".aac")
     || strstr(strlwr(filename + (len - 4)), ".wma")
     || strstr(strlwr(filename + (len - 4)), ".wav")
     || strstr(strlwr(filename + (len - 4)), ".fla")
     || strstr(strlwr(filename + (len - 4)), ".mid")
     // and anything else you want
    ) {
    result = true;
  } else {
    result = false;
  }
  return result;
}

【讨论】:

  • 感谢您的回答!!正是我拼命寻找的东西。
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 2016-05-30
  • 2011-05-07
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多