Ghost files on 32G SD card

Hello,

For some reason, my freshly-formatted 32G SD card seems to contain a lot of files, and I don't understand why. The card was formatted as FAT under MacOS, using a full-erase option that took forever to execute. And on Mac the SD card is empty (as expected). The names are not just random bytes. Could you help me understand where these file names come from? Do I have some virus, or is this smth specific to 32G cards?

I read it using the following sketch, using an Arduino Mega2560 R3:

#include "SD.h"
#include "SPI.h"

File root ;

void setup(){
  Serial.begin(9600);
  delay(1000);
  Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  if(!SD.begin(53)){
    Serial.println("Card Mount Failed");
    return;
  }
  root = SD.open("/");
  printDirectory(root, 0);
  Serial.println();

  Serial.println("Done!");
}

void loop(){
}

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');
    }

    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);
    }
  }
}

Here is the output I get:

$ arduino-cli monitor -p /dev/cu.usbmodem21101 
Connected to /dev/cu.usbmodem21101! Press CTRL-C to exit.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SPOTLI~1/
	STORE-V2/
		436B6E~1/
			PSID.DB		8192
			TM~1.LIO		0
			LIO~1.CRE		0
			TMP.CAB		0
			CA~1.CRE		0
			TM~1.STA		0
			INDEXS~1		28
			~~~~~~21.IND		68544
			~~2.IND		16
			~~~3.IND		8
			~~~~4.IND		3277
			~~~~~~30.IND		4
			~~~~~5.IND		376
			~~~~~~36.IND		2056
			~~~~~~39.IND		8
			~1.SHA		4096
			STOR~1.UPD		8
			~1.DIR		65536
			0DIREC~1.SHA		1088
			LIVE~1.IND		4096
			LIVE~~2.IND		32768
			LIVE~~~3.IND		16384
			LIVE~~~4.IND		3277
			LIVE~~~5.IND		4096
			LIVE~~71.IND		8192
			LIVE~~74.IND		4096
			LIVE~~77.IND		8192
			LIVE~~80.IND		8224
			LIVE~~83.IND		1024
			LIVE~~87.IND		65536
			LIVE~~90.IND		0
			LIVE~1.DIR		65536
			STORE.DB		36864
			STOR~1.DB		36864
			DBSTR-~1.HEA		56
			DBSTR-~1.DAT		16384
			DBSTR-~1.OFF		16384
			DBSTR-~1.BUC		16384
			DBSTR-~2.HEA		56
			DBSTR-~2.DAT		16384
			DBSTR-~2.OFF		16384
			DBSTR-~2.BUC		16384
			DBSTR-~3.HEA		56
			DBSTR-~3.DAT		16384
			DBSTR-~3.OFF		16384
			DBSTR-~3.BUC		16384
			DBSTR-~4.HEA		56
			DBSTR-~4.DAT		16384
			DBSTR-~4.OFF		16384
			DBSTR-~4.BUC		16384
			DBSTR-~5.HEA		56
			DBSTR-~5.DAT		16384
			DBSTR-~5.OFF		16384
			DBSTR-~5.BUC		16384
			REVERS~1		65536
			TMPSPO~1.STA		4096
			STORE_~1		4
			JOURNA~1.COR/
			JOURNA~1.LIV/
			JOURNA~2.LIV/
			JOURNA~3.LIV/
			JOURNA~4.LIV/
			JOURNA~1.ASS/
			JOURNA~2.ASS/
			JOURNA~1.HEA/
			JOURNA~1.MIG/
			JOURNA~2.MIG/
			JOURNA~1		0
			JOURNA~1.SCA/
			SHUTDO~1		4
			REVERS~1.SHA		3136
			~1.IND		4096
			~~~~~234.SHA		2
			LIVE~1.SHA		4096
			LIVE~~2.SHA		0
			LIVE~~~3.SHA		0
			LIVE~~~4.SHA		8
			LIVE~~~5.SHA		2056
			LIVE~255.SHA		65536
			LIVE0D~1.SHA		1088
			LIVE~262.SHA		1
			REVERS~1.UPD		2
			TMPSPO~1.LOC		2081
	VOLUME~1.PLI		4469
FSEVEN~1/
	FSEVEN~1		36
	000000~1		48
	000000~2		72

Done!

Looks like spotlight creates an index on the filesystem as soon as you format it.

1 Like

In general it's not advised to use OS tools to format the SD card. There is a dedicated tool on on sdcard.org: SD Memory Card Formatter for Windows/Mac | SD Association

Further the SD library has issues with cards over 4GB in size; I would advise to us the SDFat library.

Note that these might not address your issue.

1 Like

The SdFat library contains an example called SDFormatter that will completely erase the entire card, then format it. It works very quickly, and is far better than the app from the SD Association, which cannot erase the card, only overwrite it, which takes forever and is not what you want.

Agreed. I've had nothing but trouble with the SD Association app, and abandoned it a long time ago. For that matter, the SD library is unreliable too.

I should have said - many cameras include a reformatting function that can also erase the card. In my Canon DSLR it's called "low-level" formatting. The pre-erasure is important to cameras because of the need to write burst images or high-rez video to the card very rapidly.

Would you consider Android phone SD formatting reliable for Arduino in this topic?

Settings >> Storage >> Device care >> Storage >> SD >> menu >> Settings >> format (whew!)

Android does not leave a clean file system, nor does Windows nor MacOS. If you want it clean, use Linux or BSD.

Yes, my go-to... but I have not taken the time to look at an Android-formatted SD card on a Linux system. I was looking for user convenience. SDFormatter sounds right-size, right-place.

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