Arduino File.isDirectory()

Description

The File.isDirectory() function checks if the a opened file is a directory (or folder) or not. Please note that a directory (or folder) is a special kind of file.

Syntax

file.isDirectory()

Parameters

  • file: an instance of the File class (returned by file.open()

Returns

  • true: If the opened file is a directory (folder)
  • false: If the opened file is a file

Example Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.isdirectory */ #include <SD.h> #define PIN_SPI_CS 4 File file; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println("SD CARD FAILED, OR NOT PRESENT!"); while (1); // don't do anything more: } SD.mkdir("arduino/logs"); // create a directory // create new file by opening file for writing file = SD.open("arduino/logs/info.txt", FILE_WRITE); file.close(); file = SD.open("arduino/logs"); if (file.isDirectory()) Serial.println("arduino/logs is a directory"); else Serial.println("arduino/logs is a file"); file.close(); file = SD.open("arduino/logs/info.txt"); if (file.isDirectory()) Serial.println("info.txt is a directory"); else Serial.println("info.txt is a file"); file.close(); } void loop() { }
  • Open Serial Monitor, you will see as below:
COM6
Send
arduino/logs is a directory info.txt is a file
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES