Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:48+00:00 2026-06-17T07:41:48+00:00

How to truncate a string (containing a path) without touching the filename (keep the

  • 0

How to truncate a string (containing a path) without touching the filename (keep the last folder, the filename and the drive letter)?

Hi,
I’m looking for a way to truncate a path to fit a specified width.

I already search on SO and I found this:
Truncate a string nicely to fit within a given pixel width

But it’s adding … at the end of the string. I would like to have a similar function but it need to keep the begining and the ending..

For exemple, I have this path (string):

H:\Informatique\Personnalisation\Icones\__\Crystal GT PNG Pack\Applications\licq.png

And it need to fit the div’s width like this:

H:\Informatique\...PNG Pack\Applications\licq.png

Another exemple:

D:\A___VERY___________LONG________PATH____________\myfile.png

Will be shorten to:

D:\A___VERY___________LONG________PA...\myfile.png

Last exemple:

D:\A___VERY___________LONG________PATH____________\and-a-sub-solder\myfile.png

Will be shorten to:

D:\A___VERY________...\and-a-sub-solder\myfile.png

Restriction:

  • The drive part is required (H:)
  • The filename and the extension is required (licq.png)
  • If there’s multiple folders, the last folder must not be truncate, exept if everything is already truncated and it is the only way to
    make the string shorter.
  • If the filename is too long, truncate it in the middle.

Long filename exemple:

D:\my____file___________________________name____is___too____________long.png

Will be shorten to:

D:\my____file_________..._is___too____________long.png

I see windows and apple doing it the nice way… but I can’t find any script that is near that.

Any link with similar script so I can try adjust it?
Or anybody who wants to help me with that? 🙂

Thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:41:49+00:00Added an answer on June 17, 2026 at 7:41 am

    That’s an interesting problem, here is my solution: http://jsfiddle.net/AlienHoboken/y7SgA/

    It assumes that you have a variable str with the path in it, and maxLength with your max character length.

    You will need to change code slightly as str is hard coded, you’ll need to change maxLength, and you’ll need to change where you write the information too (it just writes to body right now).

    NB: If your drive name + file name exceed the maxLength variable, it will just print out those two together, even though they are too large.

    Code, incase you don’t like jsfiddle:

    $(document).ready(function () {
      var str = "H:\\Informatique\\Personnalisation\\Icones\\__\\Crystal GT PNG Pack\\Applications\\licq.png";
      var maxLength = 25;
    
      var tokens = str.split("\\");
      var drive = tokens[0];
      var fileName = tokens[tokens.length - 1];
      var len = drive.length + fileName.length;
      //remove the current lenth and also space for 3 dots and 2 slashes
      var remLen = maxLength - len - 5;
    
      //if remLen < 0, then it will over flow our maxLength to still maintain drive and     filename
      if (remLen > 0) {
        //remove first and last elements from the array
        tokens.splice(0, 1);
        tokens.splice(tokens.length - 1, 1);
        //recreate our path
        var path = tokens.join("\\");
        //handle the case of an odd length
        var lenA = Math.ceil(remLen / 2);
        var lenB = Math.floor(remLen / 2);
        //rebuild the path from beginning and end
        var pathA = path.substring(0, lenA);
        var pathB = path.substring(path.length - lenB);
        path = drive + "\\" + pathA + "..." + pathB + "\\" + fileName;
    
        //write it out
        $("body").html("Orig. Path: " + str + "<br /><br />" +
          "New Path: " + path + "<br /><br />" +
          "MaxLength: " + maxLength + "<br /><br />" +
          "New Length: " + path.length);
      } else {
    
        //try and fit our maxlength by taking only drive and filename
        $("body").html("Orig. Path: " + str + "<br /><br />" +
          "New Path: " + drive + "\\" + fileName + "<br /><br />" +
          "MaxLength: " + maxLength + "<br /><br />" +
          "New Length: " + (len + 1) + "<br /><br />");
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to split a string containing HTML in to two
I am looking for a way to truncate a string in Python that will
I have a string headerText, which containing text ABC (2-8 Days). How can truncate
What's the best way to truncate a string to the first n words?
I'm trying to truncate a string to a certain pixel width. The best way
Someone gave me a syntax to truncate a string as follows: string = My
Would anyone please advise how in jade for nodejs I can truncate a string
I need to truncate a comma at the end of a string, sort of
Possible Duplicate: Truncate string on whole words in .Net C# I have to display
How does one truncate a string to 75 characters in Python? This is how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.