filename.ino as string variable

Wondering ... is it possible to use the name of the .ino file as a string variable ? (with or without full path) e.g. to Serial.print (filename)

thanks in advance !!!

There are a number of predefined constants you can use, like FILE.

@PaulS - Yes but that won't fet the .ino because of the rework the IDE does.

Mark

holmes4: @PaulS - Yes but that won't fet the .ino because of the rework the IDE does.

Mark

The value is a string. A string can be copied/manipulated. The cpp file that the IDE creates has the same name as the ino file, expect for the suffix.

holmes4: @PaulS - Yes but that won't fet the .ino because of the rework the IDE does.

Mark

Try it - I have a sketch called test.ino that I paste forum problems into to avoid accumulating a plethora of files. FILE is: /Users/username/Documents/Arduino/test/test.ino. IDE 1.6.12.

Thanks for your fast reply, guys !

Still a little confused ...

Suppose my .ino file is "123.ino"

String a = ...... ->(123.ino with or without full path) Serial.print (a);

result on COM should read 123.ino (with or without full path)

Try it and see:

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
}

void loop()
{
}
char *a = __FILE__;
Serial.print(a);

Try to avoid big-S Strings if you can.

Edit: fixed 2 bugs in the code.

WOOT-WOOT !!! What would I do without you guys ?!? tanx, tanx, tanx !!!

this is my code :

String ino = FILE; Serial.println(FILE); Serial.println (ino);

on the serial monitor, now I read :

D:\ARDUINO SKETCH\SOLARii_MEGA190403a\SOLARii_MEGA190403a.ino D:\ARDUINO SKETCH\SOLARii_MEGA190403a\SOLARii_MEGA190403a.ino

THAT IS IT !!!

10K tanx !!!

MorganS: char *a = __FILE__; Serial.print();[/url]

Try to avoid big-S Strings if you can.

You probably want to print a, not an empty string. Of course, printing something before a, so you know what a pile of characters represents, would be good, too.

MorganS: char *a = __FILE__;

I'd go with:

const char * const a = __FILE__;

or

const char a[] = __FILE__;

Alright, allright ... :) tanx again !

Although I'm already happy with the answer from wildbill, when I use this code :

  String ino = __FILE__;
  Serial.println(__FILE__);
  Serial.println (ino);

  char *a = __FILE__;
  Serial.print("ino = "); [ / url];

---> the compiler tells me : "expected identifier before '/' token"...

What am I doing wrong ?

PaulVdB: What am I doing wrong ?

Not posting the complete code. Not posting the complete error message.

  Serial.print("ino = "); [ / url];

The code that MorganS posted was in malformed tags. That [/url] bit was not supposed to be part of the code.

Ah ! OK. 20K thanks PaulS !

Not posting the complete code. Not posting the complete error message.

My complete code (so far) is 343 lines... do you want me to post this whole thing here? I can ... no problem.

The complete error message is :

C:\Users\Paul\AppData\Local\Temp\arduino_modified_sketch_920279\SOLARii_MEGA190403a.ino: In function 'void setup()':

SOLARii_MEGA190403a:122: error: expected identifier before '/' token

Serial.print("ino = "); [ / url];

^

C:\Users\Paul\AppData\Local\Temp\arduino_modified_sketch_920279\SOLARii_MEGA190403a.ino: In lambda function:

SOLARii_MEGA190403a:122: error: expected '{' before ';' token

122 Serial.print("ino = "); [ / url];

^

Using library Adafruit-GFX-Library-master at version 1.3.6 in folder: D:\ARDUINO SKETCH\libraries\Adafruit-GFX-Library-master Using library MCUFRIEND_kbv at version 2.9.8 in folder: D:\ARDUINO SKETCH\libraries\MCUFRIEND_kbv Using library DS3231 in folder: D:\ARDUINO SKETCH\libraries\DS3231 (legacy) Using library Rotary-master in folder: D:\ARDUINO SKETCH\libraries\Rotary-master (legacy) Using library SPI at version 1.0 in folder: C:\Users\Paul\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.23\libraries\SPI exit status 1 expected identifier before '/' token

do you want me to post this whole thing here? I can … no problem.

More importantly, we want you to read all the replies. I explained why your code fails to compile in reply #13.

Yes, PaulS,

I've read all the posts. (especially your post #13 !) :) (and you nailed it !) ;)

the second part of my last post (#14) was meant to be for our friend gfvalvo 8) It did seem to be odd to me that "[ / url];". I thought it might have been some code to extract ONLY the filename... So I tried it and got the message that you saw after my "quote"

I guess that there's no solution to directly get only the filename except to do some "String juggling" ... But I can do that ... VBS

Again : T H A N K S to ALL !

OK, sorry to bother you again guys ...

when I want to find the last backslash in my string (C:\Users\Paul\AppData\Local\Temp\arduino_modified_sketch_141071\SOLARii_MEGA190403a.ino)

I use

int last = (ino.lastIndexOf("\"));

but then the compiler reports :

missing terminating " character ???

I noticed that the \ character does not become blue in the code ...

hmmm ... Browsed the internet and "Arduino Cookbook" from Michael Margolis ... but so far nothing found ... Anyone ? :)

Again : 30K thanks !

Probably need an Escape Sequence.

The slash character you're using has a special meaning to the compiler. You use it to 'escape' the next character in a string. You may have used \n to get a newline for instance. Consequently, when you want an actual slash, you need two of them. So you offending line becomes

int last = (ino.lastIndexOf("\\"));