www.webdeveloper.com
Results 1 to 4 of 4

Thread: cannot extract filename with extension from filepath

  1. #1
    Join Date
    Nov 2004
    Posts
    19

    Question cannot extract filename with extension from filepath

    HI,
    I have a problem......I am getting a session variable's value (which is a file path) and I am trying to extract the filename from the path.in vain.

    Here's how it looks:
    ........
    String audiofilepath;
    String newfilename;
    String filenameext;
    //Get value of the path (Say C:\myfile.wav)
    audiofilepath = (String) session.getAttribute("audiofilepath");

    //remove the files original extension and attach ".txt" extension to it (say C:\myfile.txt)
    newfilename = audiofilepath.substring( 0, audiofilepath.lastIndexOf( "." ) ) + ".txt";

    NOw I want to get only the "myfile.txt part from the newfilename, i.e it should return myfile.txt only. I am trying to do this by :

    filenameext=newfilename.match(/[^\/\\]+$/);
    However when I compile this I keep getting errors like:
    1.illegal start of expression filenameext=newfilename.match(/[^\/\\]+$/);
    ^
    2.illegal character: \92 filenameext=newfilename.match(/[^\/\\]+$/);
    ^
    3.illegal character: \92 filenameext=newfilename.match(/[^\/\\]+$/);
    ^
    4.illegal character: \92 filenameext=newfilename.match(/[^\/\\]+$/);
    ^
    5.')' expected filenameext=newfilename.match(/[^\/\\]+$/);
    ^

    I am very new to java and regular expressions . Can someone explain how I can get around this problem...an example would be really gr8.

    thanks.
    G.

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    What regex package are you using? I don't know of one that will accept that "perlish" literal syntax; they all want a regex built from strings.

  3. #3
    Join Date
    Nov 2004
    Posts
    19
    I am using java.util.regex.* .

    Is there any other way to separate filename from filepath (all I want to do is extract the path after the last occurence of "\" to the end of the path.
    thanks.

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Use
    Code:
    String [] splFileName = newfilename.split('/');
    That should return an array of String's. eg C:\images\hello.gif will return an array containing: C:, images, and hello.gif.

    So you just have to take the last index
    Code:
    String fileNoPath = splFileName[splFileName.length-1];
    That should now give you the file name.

    There are several ways to achieve this. You could grab everything after the returned value of newfilename.lastIndexOf("/");.

    Regards.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles