Get file name from URL

get filename from url javascript
get file name from url python
get file name from url php
get file name from url jquery
get file name from url android
get file name from url c#
get file name from url swift
get file name from url nodejs

In Java, given a or a String in the form of , what is the easiest way to get the file name, minus the extension? So, in this example, I'm looking for something that returns "file".

I can think of several ways to do this, but I'm looking for something that's easy to read and short.

Instead of reinventing the wheel, how about using Apache commons-io:


public class FilenameUtilTest {

    public static void main(String[] args) throws Exception {
        URL url = new URL("");

        System.out.println(FilenameUtils.getBaseName(url.getPath())); // -> file
        System.out.println(FilenameUtils.getExtension(url.getPath())); // -> xml
        System.out.println(FilenameUtils.getName(url.getPath())); // -> file.xml


Need to extract filename from URL, Need to extract filename from URL [duplicate] � java regex. This question already has answers here: Get last part of url� Get filename from URL using Javascript This snippet will get the filename from the url. The filename is the last part of the URL from the last trailing slash. For example, if the URL is then file.html is the file name.

String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );

String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.'));

js function to get filename from url, Use the match function. function GetFilename(url) { if (url) { var m = url.toString(). match(/.*\/(.+?)\./); if (m && m.length > 1) { return m[1]; } } return ""; }. Get file name from URL posted by Justin Musgrove on 19 July 2015. Tagged: java and java-net. Share on: Twitter Facebook Google+. All the code on this page is

If you don't need to get rid of the file extension, here's a way to do it without resorting to error-prone String manipulation and without using external libraries. Works with Java 1.7+:

import java.nio.file.Paths

String url = ""
String filename = Paths.get(new URI(url).getPath()).getFileName().toString()

Get filename from URL using Javascript / BeFused, The filename is the last part of the URL from the last trailing slash. For example, if the URL is then file.html is� Use urllib.parse.urlparse to get just the path part of the URL, and then use pathlib.Path on that path to get the filename:

This should about cut it (i'll leave the error handling to you):

int slashIndex = url.lastIndexOf('/');
int dotIndex = url.lastIndexOf('.', slashIndex);
String filenameWithoutExtension;
if (dotIndex == -1) {
  filenameWithoutExtension = url.substring(slashIndex + 1);
} else {
  filenameWithoutExtension = url.substring(slashIndex + 1, dotIndex);

Get filename from URL with extension – James' Desk, Get the filename from the URL or path using Javascript: Consider the URL: var url = "" Method 1:� Following steps shows total information about how to get file, file with extension, file without extension. This technique is very helpful for me. Hope it will be helpful to you too.

public static String getFileName(URL extUrl) {
        //URL: ""
        String filename = "";
        //PATH: /photos-ak-snc1/v315/224/13/659629384/s659629384_752969_4472.jpg
        String path = extUrl.getPath();
        //Checks for both forward and/or backslash 
        //NOTE:**While backslashes are not supported in URL's 
        //most browsers will autoreplace them with forward slashes
        //So technically if you're parsing an html page you could run into 
        //a backslash , so i'm accounting for them here;
        String[] pathContents = path.split("[\\\\/]");
        if(pathContents != null){
            int pathContentsLength = pathContents.length;
            System.out.println("Path Contents Length: " + pathContentsLength);
            for (int i = 0; i < pathContents.length; i++) {
                System.out.println("Path " + i + ": " + pathContents[i]);
            //lastPart: s659629384_752969_4472.jpg
            String lastPart = pathContents[pathContentsLength-1];
            String[] lastPartContents = lastPart.split("\\.");
            if(lastPartContents != null && lastPartContents.length > 1){
                int lastPartContentLength = lastPartContents.length;
                System.out.println("Last Part Length: " + lastPartContentLength);
                //filenames can contain . , so we assume everything before
                //the last . is the name, everything after the last . is the 
                String name = "";
                for (int i = 0; i < lastPartContentLength; i++) {
                    System.out.println("Last Part " + i + ": "+ lastPartContents[i]);
                    if(i < (lastPartContents.length -1)){
                        name += lastPartContents[i] ;
                        if(i < (lastPartContentLength -2)){
                            name += ".";
                String extension = lastPartContents[lastPartContentLength -1];
                filename = name + "." +extension;
                System.out.println("Name: " + name);
                System.out.println("Extension: " + extension);
                System.out.println("Filename: " + filename);
        return filename;

Path.GetFileName Method (System.IO), GetFileName(ReadOnlySpan<Char>). Returns the file name and extension of a file path that is represented by a read-only character span. GetFileName (ReadOnlySpan<Char>) Returns the file name and extension of a file path that is represented by a read-only character span. public: static ReadOnlySpan<char> GetFileName (ReadOnlySpan<char> path); C#. public static ReadOnlySpan<char> GetFileName (ReadOnlySpan<char> path);

How to get the FileName from Url in JavaScript, To get only the file name, I'll use the split() and pop() functions. <script> let url = window.location.href; let filename = url.split('/').pop� Searches a path for a file name. Syntax LPCSTR PathFindFileNameA( LPCSTR pszPath ); Parameters. pszPath. Type: PTSTR. A pointer to a null-terminated string of maximum length MAX_PATH that contains the path to search. Return value. Type: PTSTR. Returns a pointer to the address of the string if successful, or a pointer to the beginning of the path otherwise.

basename - Manual, Here is a quick way of fetching only the filename (without extension) regardless of what suffix the file has. the best way to get the filename from url is here To extract filename from the file, we use “ GetFileName () ” method of “ Path ” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

Get file names from url list with Spredsheet and Documents, In another column, other scripts take a name from these URLs. This is the if (url != "") {. var filename = SpreadsheetApp.openByUrl(links[i][0]). Python code to extract the file name from path or URL is explained below. Find the delimiter “/” position before the file name using firstpos=path.rfind(“/”) Find the last position of the file name using lastpos=len(path) Extract the file name using path[firstpos+1:lastpos].

  • YOU do realize there is no requirement for there to be a filename at the end, or even something that looks like a filename. In this case, there may or may not be a file.xml on server.
  • in that case, the result would be an empty string, or maybe null.
  • I think you need to define the problem more clearly. What about following URLS endings? ..../abc, ..../abc/, ..../abc.def, ..../abc.def.ghi, ..../abc?def.ghi
  • I think it's pretty clear. If the URL points to a file, I'm interested in the filename minus the extension (if it has one). Query parts fall outside the filename.
  • the file name is the part of the url after the last slash. the file extension is the part of the file name after the last period.
  • In version commons-io 2.2 at least you still need to manually handle URLs with parameters. E.g. ""
  • FilenameUtils.getName(url) is a better fit.
  • It seems odd to add a dependency on commons-io when easy solutions are readily available just using the JDK (see URL#getPath and String#substring or Path#getFileName or File#getName).
  • The FilenameUtils class is designed to work with Windows and *nix path, not URL.
  • Updated example to use a URL, show sample output values and use query params.