I believe I found the following method in this thread:
https://forum.arduino.cc/t/send-sketch-filename-to-serial-monitor/1174427
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
Serial.println(F( "\n" __FILE__ ));
But I've just discovered that it no longer works. Delivers Path too. Tested on several sketches and Uno, Nano and Mega2560.
I've been using it for a long time as my \Sketches path is long, so I'd like to recover it, or an alternative that others can recommend.
When you say that it does not work, what exactly do you mean ?
Do you get an error message ?
This works for me on a Nano classic
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
void setup()
{
Serial.begin(115200);
Serial.println(__FILENAME__);
}
void loop()
{
}
The output is
sketch_nov9b.ino
1 Like
It depends on which version of the IDE you're using. I use version 1.8.18, which provides the path, but other versions may or may not display it, as you’ve found. UKHeliBob has a simple workaround, give it a try!
I used IDE 2.3.3, but I wasn't aware that I used a workaround
2 Likes
Terrypin:
Delivers Path too.
You asked what I meant by “it doesn’t work”.
“Delivers path too”
I’ll try v 2 in the morning, although do want to get a fix in1.8.19.
J-M-L
November 10, 2024, 7:35am
6
On a Mac, using 1.8.19 on a UNO, if I run
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
void setup()
{
Serial.begin(115200); Serial.println();
Serial.println(__FILE__);
Serial.println(__FILENAME__);
}
void loop() {}
I correctly see the full path and the file name
/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/arduino_modified_sketch_247424/sketch_nov10a.ino
sketch_nov10a.ino
If you run on a PC, change the '/'
in the macro into '\\'
what do you see if you run that ?
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
void setup()
{
Serial.begin(115200); Serial.println();
Serial.println(__FILE__);
Serial.println(__FILENAME__);
}
void loop() {}
As mentioned in my opening post: in 1.8.19 it delivers the path too. IOW, as well as the file. Such as:
09:25:12.014 -> C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\TEMP2\TEMP2.ino
Which for me is 'incorrectly' It's neither what was described in the thread I referenced nor what it has been doing on my Win 10 PC until very recently.
Of course, it's no big deal: I can just return to scrolling right to check the filename. But my post is mainly because it seems something has changed .
I've just updated my rarely used v2 to the latest, 2.33. So later today I'll try to reproduce the 'correct' result that @UKHeliBob got.
J-M-L
November 10, 2024, 10:21am
8
Did you run exactly this ?
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
void setup()
{
Serial.begin(115200); Serial.println();
Serial.println(__FILE__);
Serial.println(__FILENAME__);
}
void loop() {}
J-M-L:
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
void setup()
{
Serial.begin(115200); Serial.println();
Serial.println(__FILE__);
Serial.println(__FILENAME__);
}
void loop() {}
Yes, exactly.
Abandoning the Sunday paper, I then ran it on 2.33 and got this very interesting result in its serial monitor:
C:\Users\terx\Electronics\Arduino\SKETCHES\TEMP_JML\TEMP_JML.ino
TEMP_JML.ino
C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\TEMP_JML\TEMP_JML.ino
TEMP_JML.ino
As you see, it's mangled a section of the path. Hopefully a clue?
J-M-L
November 10, 2024, 10:37am
10
can you try again with
#define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
void setup()
{
Serial.begin(115200); Serial.println();
Serial.println("-------------------------------");
Serial.print("Full path: [");
Serial.print(__FILE__);
Serial.println("]");
Serial.print("File name only: [");
Serial.print(__FILENAME__);
Serial.println("]");
Serial.println("-------------------------------");
}
void loop() {}
I'd like to make sure what gets printed by what
1 Like
Hi @Terrypin
You introduced an error into the code. If you look at the code from the referenced topic:
You can see that the macro referenced in the Serial.println
call is named __FILENAME__
. But in your sketch you instead referenced the __FILE__
macro:
1 Like
Is the problem that in the original post, you defined '__FILENAME__
', but then went on to print '__FILE__
', rather than what you had just defined?
1 Like
@ptillisch , @JohnLincoln
Yes, thanks both, that's the puzzle solved!
To understand where I introduced that unwanted change I've looked again at that year old source, post #10 by @ninja2 :
Still not sure what initially changed my working two-liner, after a year or so. But that sent me looking for the original source and I'm guessing that's when I changed it to FILE . If so, at least I added the missing semi-colon.
1 Like
That works perfectly, thanks for your patience!
In both 1.8.19 and 2.3.3.
See also my partial explanation a few minutes ago. Also, I have a guess that might explain why I re-opened the topic at all.
I suspect it was while doing a Find/Replace in some sketch. I add that two-liner to most sketches. (I actually type '=fk', and a shortkey macro types the successive lines; that's why the #define isn't outside setup() as it logically should be.) Who knows?
system
Closed
May 9, 2025, 5:49pm
15
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.