snipt

Ctrl+h for KB shortcuts

JavaScript

Get File Name from URL

1
2
3
var getFileName = function (url) {
    return url.replace(/\?.*$/, "").replace(/.*\//, "");
}
https://snipt.net/embed/dba4844455e27d7a020ba19ff08e3a49/
https://snipt.net/raw/dba4844455e27d7a020ba19ff08e3a49/
dba4844455e27d7a020ba19ff08e3a49
js
JavaScript
3
2016-09-24T07:11:05
True
False
False
Oct 05, 2015 at 11:03 AM
/api/public/snipt/141505/
get-file-name-from-url-40c6beb7
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">getFileName</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="nx">url</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\?.*$/</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/.*\//</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
filename, url

Bash

Get filename and extension in bash

1
2
3
4
path=$(dirname "$full_file_path")
filename=$(basename "$full_file_path")
extension="${filename##*.}"
filename="${filename%.*}"
https://snipt.net/embed/6ae3c577f6b90fd82f3d3df508da2a69/
https://snipt.net/raw/6ae3c577f6b90fd82f3d3df508da2a69/
6ae3c577f6b90fd82f3d3df508da2a69
bash
Bash
4
2016-09-22T05:43:47
True
False
False
/api/public/snipt/49042/
get-filename-and-extension-in-bash
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">path</span><span class="o">=</span><span class="k">$(</span>dirname <span class="s2">&quot;</span><span class="nv">$full_file_path</span><span class="s2">&quot;</span><span class="k">)</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">filename</span><span class="o">=</span><span class="k">$(</span>basename <span class="s2">&quot;</span><span class="nv">$full_file_path</span><span class="s2">&quot;</span><span class="k">)</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">extension</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">filename</span><span class="p">##*.</span><span class="si">}</span><span class="s2">&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">filename</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">filename</span><span class="p">%.*</span><span class="si">}</span><span class="s2">&quot;</span> </span></pre></div> </td></tr></table>
bash, extension, filename

PHP

Cleans up file names, strips invalid characters

$filename = preg_replace('/[^0-9a-z\.\_\-]/i','',$original_filename);
https://snipt.net/embed/ab00a416fb98ce5032c1f64c1ac5d937/
https://snipt.net/raw/ab00a416fb98ce5032c1f64c1ac5d937/
ab00a416fb98ce5032c1f64c1ac5d937
php
PHP
1
2016-09-23T10:35:58
True
False
False
/api/public/snipt/9408/
cleans-up-file-names-strips-invalid-characters
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">$filename = preg_replace(&#39;/[^0-9a-z\.\_\-]/i&#39;,&#39;&#39;,$original_filename);</span> </span></pre></div> </td></tr></table>
file, filename, name, strip

PHP

returns just the filename from a path string

1
2
3
$name = "somefilenamestring.php";
$ext = strrchr($name, '.');
if($ext !== false) $name = substr($name, 0, -strlen($ext));
https://snipt.net/embed/9819e07cc1fd777c9f274ae4dd9354d2/
https://snipt.net/raw/9819e07cc1fd777c9f274ae4dd9354d2/
9819e07cc1fd777c9f274ae4dd9354d2
php
PHP
3
2016-09-22T02:27:02
True
False
False
/api/public/snipt/9364/
returns-just-the-filename-from-a-path-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">$name = &quot;somefilenamestring.php&quot;;</span> </span><span id="L-2"><a name="L-2"></a><span class="x">$ext = strrchr($name, &#39;.&#39;);</span> </span><span id="L-3"><a name="L-3"></a><span class="x">if($ext !== false) $name = substr($name, 0, -strlen($ext));</span> </span></pre></div> </td></tr></table>
extension, file, filename, name, strip

Bash

Looping through files in Bash

1
2
3
4
for FILENAME in /home/user/*.txt
do
    echo "Filename: $FILENAME"
done
https://snipt.net/embed/a2d4c41322df9e95425c295ca662fc9d/
https://snipt.net/raw/a2d4c41322df9e95425c295ca662fc9d/
a2d4c41322df9e95425c295ca662fc9d
bash
Bash
5
2016-09-21T13:41:12
True
False
False
/api/public/snipt/892/
looping-through-files-in-bash
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">for</span> FILENAME in /home/user/*.txt </span><span id="L-2"><a name="L-2"></a><span class="k">do</span> </span><span id="L-3"><a name="L-3"></a> <span class="nb">echo</span> <span class="s2">&quot;Filename: </span><span class="nv">$FILENAME</span><span class="s2">&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="k">done</span> </span></pre></div> </td></tr></table>
bash, filename, loop
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.