SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    filename path stripping

    Anyone know if Javascript comes with a function to strip a filename from a full pathname? I've been spoiled by php for too long...!

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <SCRIPT>
    var myString = "C:/My Documents/My Music/song.mp3";
    var temp = myString.split("/");
    alert(temp[temp.length-1]); //alerts "song.mp3"
    </SCRIPT>

    Now if anybody knows how to make this work with a backslash...

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "\" by itself in a JavaScript string is a metacharacter, used for things like \n (newline), \t (tab), etc.

    To use a real backslash in a JavaScript string, double-up the slash - i.e:
    Code:
    var myfilepath = "c:\\program files\\my prog\\this.doc"
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I knew that, and the script would look like this:
    <SCRIPT>
    var myString = "C:\\My Documents\\My Music\\song.mp3";
    var temp = myString.split("\\");
    alert(temp[temp.length-1]); //alerts "song.mp3"
    </SCRIPT>

    I was just wondering how to replace the \ into \\ in the following input:
    "C:\My Documents\My Music\song.mp3"
    But on second thought, javascript wouldn't accept that anyway, and would read it as:
    "C:My DocumentsMy Musicsong.mp3"

    So I guess that's it then... if you have to format the input before handing it to javascript, could as well 'strip' the filename before too...


Bookmarks

Posting Permissions

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