Deleting 100 chars of file name

I usually start setup() with this code:

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println(F("=============================================="));
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
etc

But my sketches are in a deep subfolder of my Dropbox folder so I always have to scroll horizontally to read the short sketch name. Can anyone suggest an edit to that
Serial.println( F(__FILE__) );
line which would delete say the first 100 characters please?

try something like this

const char * compileFilename = __FILE__;

void setup() {
  Serial.begin(115200);
  Serial.print("Full path : "); Serial.println(compileFilename);
  // look for the last /
  const char * lastSlashPtr = strrchr(compileFilename, '/'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
  if (lastSlashPtr  != nullptr) {
    Serial.print("File name : "); Serial.println(lastSlashPtr);
  }
}

void loop() {}

if you are on windows you might need to change the '/' into '' I don't know what the __FILE__ #define looks like

of course you could make it shorter

  Serial.println( F("Code running comes from file ") );
  Serial.println( strrchr(__FILE__, '/'));

(drawback is that there is a memory impact on AVR as you have the text in RAM for string literals)

Thanks but couldn't get any version of that working.

const char * compileFilename = __FILE__;

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("=============================================");
  Serial.print("Full path : ");
  Serial.println(compileFilename);
  // look for the last /
  const char * lastSlashPtr = strrchr(compileFilename, '/'); // Orig
  // https://en.cppreference.com/w/cpp/string/byte/strrchr
  if (lastSlashPtr  != nullptr)
  {
    Serial.print("File name : ");
    Serial.println(lastSlashPtr);
  }
}

void loop()
{
}

/*
  Output:
  Full path : C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test\JML-Test.ino
  No short version printed
*/

const char * compileFilename = FILE;

I am using Windows so then tried changing '/' to ''. The IDE didn't like that!

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("=============================================");
  Serial.print("Full path : ");
  Serial.println(compileFilename);
  // look for the last \
  const char * lastSlashPtr = strrchr(compileFilename, '\');
                                      // https://en.cppreference.com/w/cpp/string/byte/strrchr
                                      if (lastSlashPtr  != nullptr)
                                      {
                                      Serial.print("File name : ");
                                      Serial.println(lastSlashPtr);
                                    }
                                    }

                                      void loop()
                                      {
                                    }

                                      /*
                                      Weird format! (Ctrl +T no change)

                                      ERROR
                                      'lastSlashPtr' was not declared in this scope
                                      */

Using double quotes didn't help either.

Also tried adding the #include statements from the article you referenced:

#include
#include

Or this, which is my normal syntax?
#include <iostream.h>
#include <cstring.h>

But neither were accessible in the IDE.

Didn't really follow your "of course you could make it shorter"? By 'it' did you mean the code, not the printed result? FWIW I tried this without success:

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("=============================================");
  Serial.println( F("Code running comes from file ") );
  Serial.println( strrchr(__FILE__, '\'));
  }

void loop()
{
}

/*
 * ERROR
  missing terminating 'character
*/

Sorry forgot to mention anti slash has to doubled as it’s an escape character…


const char * compileFilename = __FILE__;

void setup() {
  Serial.begin(115200);
  Serial.print("Full path : "); Serial.println(compileFilename);
  // look for the last \
  const char * lastSlashPtr = strrchr(compileFilename, '\\'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
  if (lastSlashPtr  != nullptr) {
    Serial.print("File name : "); Serial.println(lastSlashPtr);
  }
}

void loop() {}

I meant the code would be shorter if you include everything in the print statement


Serial.println( F("Code running comes from file ") );
Serial.println( strrchr(__FILE__, '\\'));

That gave the error
'lastSlashPtr' was not declared in this scope

const char * compileFilename = __FILE__;

void setup() {
  Serial.begin(115200);
  Serial.print("Full path : "); Serial.println(compileFilename);
  // look for the last \
  const char * lastSlashPtr = strrchr(compileFilename, '\\'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
  if (lastSlashPtr  != nullptr) {
    Serial.print("File name : "); Serial.println(lastSlashPtr);
  }
}

void loop() {}

Here's the latter part of the full error report; let me know if you need more.

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino:6:3: warning: multi-line comment [-Wcomment]

   // look for the last \

   ^

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino: In function 'void setup()':

JML-Test-1a:8:7: error: 'lastSlashPtr' was not declared in this scope

   if (lastSlashPtr  != nullptr) {

       ^~~~~~~~~~~~

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\MISC\JML-Test-1a\JML-Test-1a.ino:8:7: note: suggested alternative: 'strcasestr'

   if (lastSlashPtr  != nullptr) {

       ^~~~~~~~~~~~

       strcasestr

exit status 1

'lastSlashPtr' was not declared in this scope

I

lol forgot that an antislash at the end of the line tells the compiler to continue as if there was no break... hence the greyed out definition of lastSlashPtr which is part of the comment now

const char * compileFilename = __FILE__;

void setup() {
  Serial.begin(115200);
  Serial.print("Full path : "); Serial.println(compileFilename);
  // look for the last antishlash '\' in our file name
  const char * lastSeparator = strrchr(compileFilename, '\\'); // https://en.cppreference.com/w/cpp/string/byte/strrchr
  if (lastSeparator  != nullptr) {
    Serial.print("File name : "); Serial.println(lastSeparator);
  }
}

void loop() {}
1 Like

Excellent, works perfectly now. Thanks for your patient help.

great - have fun

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