user80933: I want to extract the file name MyFile.txt out of a path which is stored in a string "C:\\MyDirectory\\MyFile.txt";
I found a simple c code which does the job.
string filename = "C:\\MyDirectory\\MyFile.bat";

// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = filename.find_last_of("\\/");
if (std::string::npos != last_slash_idx)
{
    filename.erase(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = filename.rfind('.');
if (std::string::npos != period_idx)
{
    filename.erase(period_idx);
}

I had to alter few things for arduino code
example replace find_last_of with lastIndexOf, erase with remove and rfind with lastIndexOf
Here the ino file
#include<string>
void setup() {
  Serial.begin(9600);

}

void loop() {   
   String filename = "C:\MyDirectory\MyFile.bat";

// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = filename.lastIndexOf("\\");
if (std::string::npos != last_slash_idx)
{
    filename.remove(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = filename.lastIndexOf('.');
if (std::string::npos != period_idx)
{
    filename.remove(period_idx);
}  
Serial.println(filename);
delay(1000);
}

Expected output MyFile
When I look into Serial Monitor I get
c$$⸮'<;⸮⸮/<⸮⸮$⸮''⸮;,#$$⸮'<;⸮⸮'<⸮⸮$⸮''⸮:$#$$⸮'<;⸮⸮'<⸮⸮$⸮''⸮;$#$$⸮'

What am I doing wrong?

user7508: 
#include<string>


Remove this. Arduino uses its own String class, different from the STL
string.

String filename = "C:\MyDirectory\MyFile.bat";


There is no '\M' escape sequence in C++ strings. You probably mean
"C:\\MyDirectory\\MyFile.bat".

const size_t last_slash_idx = filename.lastIndexOf("\\");


String::lastIndexOf() returns an int, not size_t.

if (std::string::npos != last_slash_idx)


No std::string here. String::lastIndexOf() returns -1 to signify
that the character was not found.
With all these fixes, your sketch should work as intended.

80933: <p>I want to extract the file name <code>MyFile.txt</code> out of a path which is stored in a string <code>&quot;C:\\MyDirectory\\MyFile.txt&quot;;</code></p>
<p>I found a simple <a href="https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path?answertab=votes#tab-top?#answer-8520815">c code</a> which does the job.</p>
<pre><code>string filename = &quot;C:\\MyDirectory\\MyFile.bat&quot;;

// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = filename.find_last_of(&quot;\\/&quot;);
if (std::string::npos != last_slash_idx)
{
    filename.erase(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = filename.rfind('.');
if (std::string::npos != period_idx)
{
    filename.erase(period_idx);
}
</code></pre>
<p>I had to alter few things for arduino code
example replace <code>find_last_of</code> with <code>lastIndexOf</code>, <code>erase</code> with <code>remove</code> and <code>rfind</code> with <code>lastIndexOf</code></p>
<p>Here the <code>ino</code> file</p>
<pre><code>#include&lt;string&gt;
void setup() {
  Serial.begin(9600);

}

void loop() {   
   String filename = &quot;C:\MyDirectory\MyFile.bat&quot;;

// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = filename.lastIndexOf(&quot;\\&quot;);
if (std::string::npos != last_slash_idx)
{
    filename.remove(0, last_slash_idx + 1);
}

// Remove extension if present.
const size_t period_idx = filename.lastIndexOf('.');
if (std::string::npos != period_idx)
{
    filename.remove(period_idx);
}  
Serial.println(filename);
delay(1000);
}
</code></pre>
<p>Expected output <code>MyFile</code></p>
<p>When I look into Serial Monitor I get</p>
<pre><code>c$$⸮'&lt;;⸮⸮/&lt;⸮⸮$⸮''⸮;,#$$⸮'&lt;;⸮⸮'&lt;⸮⸮$⸮''⸮:$#$$⸮'&lt;;⸮⸮'&lt;⸮⸮$⸮''⸮;$#$$⸮'
</code></pre>
<p>What am I doing wrong?</p>
 7508: <blockquote>
<pre class="lang-cpp prettyprint-override"><code>#include&lt;string&gt;
</code></pre>
</blockquote>
<p>Remove this. Arduino uses its own <code>String</code> class, different from the STL
<code>string</code>.</p>
<blockquote>
<pre class="lang-cpp prettyprint-override"><code>String filename = &quot;C:\MyDirectory\MyFile.bat&quot;;
</code></pre>
</blockquote>
<p>There is no <code>'\M'</code> escape sequence in C++ strings. You probably mean
<code>&quot;C:\\MyDirectory\\MyFile.bat&quot;</code>.</p>
<blockquote>
<pre class="lang-cpp prettyprint-override"><code>const size_t last_slash_idx = filename.lastIndexOf(&quot;\\&quot;);
</code></pre>
</blockquote>
<p><code>String::lastIndexOf()</code> returns an <code>int</code>, not <code>size_t</code>.</p>
<blockquote>
<pre class="lang-cpp prettyprint-override"><code>if (std::string::npos != last_slash_idx)
</code></pre>
</blockquote>
<p>No <code>std::string</code> here. <code>String::lastIndexOf()</code> returns <code>-1</code> to signify
that the character was not found.</p>
<p>With all these fixes, your sketch should work as intended.</p>
