Best Way to Remove File Extension

best way to remove file extension

Read the documentation of File::basename :

basename(file_name [, suffix] ) → base_name

Returns the last component of the filename given in file_name, which can be formed using both File::SEPARATOR and File::ALT_SEPARETOR as the separator when File::ALT_SEPARATOR is not nil. If suffix is given and present at the end of file_name, it is removed.

file = "/home/usr/my_file.xml"
File.basename(file,File.extname(file)) # => "my_file"

How to trim a file extension from a String in JavaScript?

If you know the length of the extension, you can use x.slice(0, -4) (where 4 is the three characters of the extension and the dot).

If you don't know the length @John Hartsock regex would be the right approach.

If you'd rather not use regular expressions, you can try this (less performant):

filename.split('.').slice(0, -1).join('.')

Note that it will fail on files without extension.

How do I remove file extensions from a lot of files?

forfiles /S /M *.ext /C "cmd /c rename @file @fname" 

Found here.

This code renames all files in a folder and the subfolder recursivly when the command /S is used. The command /M defines the search mask to find the desired file type, which will be renamed. The /C defines that following code has to be executed on all desired files in a loop. More infos on the type of parameters can be found in the Microsoft documentation.

Proper way to remove file extensions

You can use these two rules for effectively removing .php extension and taking care of SEO rankings:

RewriteEngine On

# To externally redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally redirect /file OR file/ to /file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Due to first 301 redirection search engines will not cache /file.php page and only /file will be cached in search results.

Remove file extension from a file name string

I used the below, less code

string fileName = "C:\file.docx";
MessageBox.Show(Path.Combine(Path.GetDirectoryName(fileName),Path.GetFileNameWithoutExtension(fileName)));

Output will be

C:\file

Remove the extension of a file

Something like

if (name.indexOf(".") > 0)
name = name.substring(0, name.lastIndexOf("."));

The index check avoids turning hidden files like ".profile" into "", and the lastIndexOf() takes care of names like cute.kitty.jpg.

how to remove file extension for display, but retaining extension for href?

easier way to do this using PHP's ScanDir

http://php.net/manual/en/function.scandir.php

$files =  array_diff(scandir('folder/'), array('.', '..'));

foreach($files as $file){

$name = strstr($file, '.', TRUE);
echo '<li><a class="" href="folder/'.$file.'">'.$name.'</a></li>';

}

How can I remove the extension of a filename in a shell script?

You should be using the command substitution syntax $(command) when you want to execute a command in script/command.

So your line would be

name=$(echo "$filename" | cut -f 1 -d '.')

Code explanation:

  1. echo get the value of the variable $filename and send it to standard output
  2. We then grab the output and pipe it to the cut command
  3. The cut will use the . as delimiter (also known as separator) for cutting the string into segments and by -f we select which segment we want to have in output
  4. Then the $() command substitution will get the output and return its value
  5. The returned value will be assigned to the variable named name

Note that this gives the portion of the variable up to the first period .:

$ filename=hello.world
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello.hello.hello
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello
$ echo "$filename" | cut -f 1 -d '.'
hello

Remove file extension from full path url Javascript

You're probably looking for substring.
Check the code below, and if you want more information about reduce, check this link from MDN.

function createImagesTag(data){
const images = JSON.parse(data);
const imageList = images.reduce((acc, el, index) => {
const srcWithoutSuffix = el.substring(0, el.length - 4);
if(index === 0) {
acc+=`<video muted preload='none' poster='${srcWithoutSuffix}'' width='80%' height='40%' id='images_"+a+"' onclick='changeSelected(0)' class='imagescards' <source src='${el}'#t=1.5'type='video/mp4' style='border: 3px solid red;'></video><br />`;
return acc;
}

acc+=`<video muted preload='none' poster='${srcWithoutSuffix}' width='80%' height='40%' id='images_"+a+"' onclick='changeSelected(${index})' class='imagescards' <source src='${el}'#t=1.5' type='video/mp4'></video><br />`
return acc;
}, '');

const imagesHTML = imageList.substr(0, imageList.length - 4);
document.getElementById("images").innerHTML = imagesHTML;

}


Related Topics



Leave a reply



Submit