Jun 8, 2025

Getting file name and path and compile date, update

Quickly identify which firmware version is running on your Arduino or Teensy by printing the file name, path, and compile timestamp at startup. Call these functions from setup().  (This is an update to an earlier File & Path post.)

It's especially useful when you have a board with code, but you don't know what it is or where to find the source; seeing this at the start is helpful to find it. Here's a typical output:

 Path: C:\Users\you\Documents\Arduino\
 File: Example_Program.ino
 Compiled Jun 04 2025 at 22:31:48
 Version  v1.2.1
 

// Print filename and path from __FILE__
void printFilePath() {
  String fullPath = String(__FILE__);
  int split = fullPath.lastIndexOf("\\") + 1;
  String fileName = fullPath.substring(split);
  String filePath = fullPath.substring(0, split);

  snprintf(outputMsg, sizeof(outputMsg), "  Path: %s", filePath.c_str()); Serial.println(outputMsg);
  snprintf(outputMsg, sizeof(outputMsg), "  File: %s", fileName.c_str()); Serial.println(outputMsg);
}

// Print build date/time and firmware version
void printCompileInfo() {
  snprintf(outputMsg, sizeof(outputMsg), "  Compiled %s at %s", __DATE__, __TIME__); Serial.println(outputMsg);
  snprintf(outputMsg, sizeof(outputMsg), "  Version  %s", VERSION_STRING); Serial.println(outputMsg);
}

// In setup():
printFilePath();
printCompileInfo();

No comments: