Capture and extract the filename and path for the current file within an Arduino program.
There are a few variables available at compile time including compile time and date. The filename is also available and supplied with its full path. The code below shows a way to separate them into String variables.Code:
/* get the file name and path of this program */
const char this_file[] PROGMEM = __FILE__; // provided by system
const String file_and_path = String(this_file); // convert to a String
int n = file_and_path.length();
int i = file_and_path.lastIndexOf("\\") + 1; // escaped backslash for Windows paths
String filename = file_and_path.substring(i,n); // from last '\' to end
String filepath = file_and_path.substring(0,i); // from begin to last '\'
void setup() {
Serial.begin(115200);
while (!Serial) {}
Serial.println(file_and_path);
Serial.println(filepath);
Serial.println(filename);
}
void loop() {
}
Output:
C:\Users\david1\Desktop\test\test.ino
C:\Users\david1\Desktop\test\
test.ino
No comments:
Post a Comment