ESP32, SD.mkdir and multiple folders not working

The Arduino docs on SD.mkdir state that SD.mkdir("a/b/c") would create all the intermediate folders.

When I try this on the ESP32, the mkdir fails.

I discovered that SD.mkdir("/a"), then SD.mkdir("/a/b"), then finally SD.mkdir("a/b/c") will accomplish what I want. NOTE, the fore slash is required in all three for it work.

Is this documented anywhere?

Can you provide a basic example that demonstrates the issue. I do not have any ESP32 boards so will not be able to verify your observation.

Be aware that the SD library in the ESP32 package is different from the standard SD library used by Arduino and hence the Arduino documentation might not apply to libraries for ESP32 based boards.

I did compile an example code (file/examples/examples for ESP32 Dev Module/SD/SD_Test) provided for the ESP32 (board ESP32 Dev Module) and it indicates that it uses C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\libraries\SD and not the standard SD library C:\Users\bugge\AppData\Local\Arduino15\libraries\SD

FQBN: esp32:esp32:esp32
Using board 'esp32' from platform in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7
Using core 'esp32' from platform in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7

...
...

Multiple libraries were found for "SD.h"
  Used: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\libraries\SD
  Not used: C:\Users\bugge\AppData\Local\Arduino15\libraries\SD
  Not used: C:\Users\bugge\OneDrive\Documents\Arduino\libraries\SD
Using library FS at version 3.0.7 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\libraries\FS 
Using library SD at version 3.0.7 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\libraries\SD 
Using library SPI at version 3.0.7 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.7\libraries\SPI 

Looking a bit further, the ESP32 SD library does not contain a function mkdir. That mkdir function is in the FS library provided in the ESP32 board package (see the example).

Which version of the ESP32 board package are you using?

Espressif ESP32 board support, v3.0.5 (latest released). Where are you getting 3.0.7?
I have tried it on it a ESP32 & ESP32S3.

Just take that SD_Test, and add this line at 245 above the first listDir call.

createDir(SD, "/a/b/c");

it will fail.

change it to the following and it will work

createDir(SD, "/a");
createDir(SD, "/a/b");
createDir(SD, "/a/b/c");

yeah, ESP has always been about compatibility and not direct code reuse, often for good reasons.

Additional URL https://espressif.github.io/arduino-esp32/package_esp32_index.json

As said, I do not have any ESP32 based boards so I can't test. I will try to find time to verify with an AVR based board.

Tested it with a Sparkfun Pro Micro. I used the Files example that comes with the built-in library for Arduino boards and added the mkdir at the end.

#include <SD.h>

// sterretje adjusted for current setup
const int chipSelect = A0;
File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // wait for Serial Monitor to connect. Needed for native USB port boards only:
  while (!Serial)
    ;

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect))
  {
    Serial.println("initialization failed. Things to check:");
    Serial.println("1. is a card inserted?");
    Serial.println("2. is your wiring correct?");
    Serial.println("3. did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset button on the board and reopen this serial monitor after fixing your issue!");
    while (1)
      ;
  }
  Serial.println("initialization done.");

  if (SD.exists("example.txt"))
  {
    Serial.println("example.txt exists.");
  }
  else
  {
    Serial.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt"))
  {
    Serial.println("example.txt exists.");
  }
  else
  {
    Serial.println("example.txt doesn't exist.");
  }

  // delete the file:
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");

  if (SD.exists("example.txt"))
  {
    Serial.println("example.txt exists.");
  }
  else
  {
    Serial.println("example.txt doesn't exist.");
  }

  // sterretje added for topic 1318705
  bool result = SD.mkdir("/a/b/c");
  if (result == false)
  {
    Serial.println("Failed to create directory '/a/b/c'");
  }
  else
  {
    Serial.println("Directory '/a/b/c' created");
  }
}

void loop()
{
  // nothing happens after setup finishes.
}

The output shows that the directory is created and I have verified it on a PC (directory names were in capitals).

So this is an issue with the ESP32 board package. I do not know if the Arduino® Nano ESP32 — Arduino Official Store suffers from the same issue; it will if Arduino did not modify the SD library for that board in which case the documentation is not correct / incomplete.

I also don't have that board but a compile shows that it uses the SD library from the Nano ESP32 board package.

Using library SD at version 2.0.0 in folder: C:\Users\bugge\AppData\Local\Arduino15\packages\arduino\hardware\esp32\2.0.18-20240930.arduino3\libraries\SD 

I tried to check documentation on Libraries - - — Arduino ESP32 latest documentation but could not find a reference to the SD library. I also checked GitHub - espressif/arduino-esp32: Arduino core for the ESP32 to find your specific problem but could not find it.

Well, I am glad I checked, after the last Arduino IDE update, my URLs got completely cleared! Added it back and yes, 3.0.7 is the latest. I will test it and if it is fixed I will reply back.

Issue still active with the latest release. So must be an ESP32 issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.