jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Q Promoted How do I convert long to short pathnames?
Topic: IO
Denis Ballant, Feb 22, 2002  [replies:7]
How do I get the short pathnames (8.3) on a Windows system?

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 1



Answer this question  


Re: How do I convert long to short pathnames?
Topic: IO
Heinrich Soebke, Mar 4, 2002
not really a solution, but the brute force attack: use Runtime.exec() and parse the output.
On Windows NT there's an option of the dir command (/X), which shows the short names.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  
Re: How do I convert long to short pathnames?
Topic: IO
Alessandro A. Garbagnati PREMIUM, Mar 12, 2002
Denis,
I'm afraid that the best solution is write a native interface to talk with the operating system, since that is something that is very WinOS dependant.

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  
Re: How do I convert long to short pathnames?
Topic: IO
Andrey Karachoun, Nov 25, 2002  [replies:4]
I think the best way is to write your own piece of code that converts long pathnames to short ones, taking first 6 characters of each file/folder of the path and adding "~1" to them.
To do this, it is better for you to create first a java.util.StringTokenizer from your pathname String and then write smth like this:
        String shortpath = "";
        StringTokenizer tokenizer = new StringTokenizer(longpath, "\\");
        while(tokenizer.hasMoreTokens() == true)
        {
            String temp = tokenizer.nextToken();
            if(temp.length() >= 8)
                temp = temp.substring(0, 6) + "~1";
            shortpath += temp + "\\";
        }


Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  
Re[2]: How do I convert long to short pathnames?
Topic: IO
Christopher Koenigsberg PREMIUM, Nov 25, 2002  [replies:3]

There is Microsoft's algorithm, for converting a regular (long) filename to a DOS (short) filename, which you can probably find on MSDN. The most basic operation is indeed to construct an "8.3" abbreviation by appending "~1" to the first 6 chars, then dropping everything else, up to the first (?) ".", and taking only the first 3 chars after that, for the "extension". But the algorithm continues, if you have a lot of files with similar names, for instance (after you run out of "~2", "~3", ...).

But then, there is the entirely different operation, of taking a directory in a FAT32/NTFS filesystem, and getting the actual (short and long) names stored for the files in it (as in "DIR /X"), IF the filesystem has generated a short name for each file (I think in the NTFS version used in WindowsXP, the short name is no longer generated and stored, by default, for every file?).

So, using Microsoft's algorithm you can predict what the short name should be, but retrieving the actual short name from the directory is a different operation. The two should match, except in the newer filesystems e.g. NTFS under WinXP, where there may no longer be a short name stored for a given file (I think you can still create and store the short name for a given file, on demand if needed?).



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  
Re[3]: How do I convert long to short pathnames?
Topic: IO
Rick LaFleur, Jun 22, 2004  [replies:2]

Caution, I think there may be a problem with this solution (not that I know a better one) or at least there was when I looked into this last.

First is handling spaces ... you need to know to drop em. So if you create a folder called "this is the first test" it will have a short name of THISIS~1.

The second issue though is more tricky. If originally there were three folders called:

"this is the first test"
"this is the second test"
"this is the third test"

They would have short names of THISIS~1, THISIS~2, and THISIS~3. So far, fine.

But, what happens if the "this is the first test" folder is deleted? On my system I show the two remaining folders listed as THISIS~2 and THISIS~3. That is, the short name seems to be cooked in when the folder is created.

Which you might think is good if you are hard coding the path name into your application.

However, if you come along later and see only the two folders, you might assume they were ~1 and ~2, not ~2 and ~3, and using the ~2 to access the 'second' directory would result in the wrong one.

Worse, if you create a new folder called "this is the fourth test" it's short name is THISIS~1, not ~4. You can just imagine what that would do to your application.

Confusing, huh?

Best solution I've found (and hope there is a better one) is the dumb dir /X command and parse results.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  

Re[4]: How do I convert long to short pathnames?
Topic: IO
Christopher Koenigsberg PREMIUM, Jun 22, 2004  [replies:1]

I would be searching at MSDN and Technet (microsoft.com) if I were needing some serious information about this ....

As I wrote (a year and a half ago! nice long conversation here :-), the NTFS filesystem has a second name slot, for storing a "short" ("8dot3") name, for a file, BUT it may be empty.
And I remember reading somewhere, that under Windows XP now (maybe under Windows 2000 too? unlike NT4 and earlier), the default is NOT to compute and store a short name anymore, unless you change a configuration parameter for the filesystem (? I forget; maybe it was an argument to the "CONVERT" command?).

If you do "HELP DIR" in a command prompt, look at the comment for the "/X" option. In Windows 2000 now it says:

This displays the short names generated for non-8dot3 file names.
The format is that of /N with the short name inserted before the long name.
If no short name is present, blanks are displayed in its place.

So you could indeed get into a situation like the one described, where short names were generated at one time, but then files were removed, etc., and your current computation of short names could be off, from the actual stored previously computed short names, because of the way they depend on sequential numbers ...... pretty yucky.



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  
Re[5]: How do I convert long to short pathnames?
Topic: IO
Christopher Koenigsberg PREMIUM, Jun 22, 2004

5 minutes at microsoft.com will come up with:

1. registry key "NtfsDisable8dot3NameCreation"

2. Win32 API function "GetShortPathName" (<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/getshortpathname.asp?frame=true>),
including remark "When an application calls this function and specifies a path on a volume that does not support 8.3 aliases, the function fails with ERROR_INVALID_PARAMETER if the path is longer than 67 bytes."

... and so on ...



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  


Ask A Question



Related Links

IO FAQ

IO Forum

I/O in Java 2 SDK

Java I/O package documentation

Lesson: Reading and Writing (Sun Tutorial)

jGuru Serialization FAQ

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers