473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DOS Short Names

If I have a strig that represents a path, how can I programmatically get the
"short" DOS name for it?

For example, if I have "C:\Program Files\" I want "C:\Progra~1\".

Thanks
Feb 1 '07 #1
14 7124
If I have a strig that represents a path, how can I programmatically get
the "short" DOS name for it?

For example, if I have "C:\Program Files\" I want "C:\Progra~1\".
AFAIK, there's no native .NET function to do this. You can simply rely on
"GetShortPathName()" in the WinAPI assuming your app is targetted for
Windows (and which .NET app isn't). Here's the first example when I googled
for "C#" and "GetShortPathName()" (many more exist)

http://www.pinvoke.net/default.aspx/...tShortPathName
Feb 1 '07 #2
Cheat :)

public string FakeDosFileString(string myStr)
string[] myChildren = myStr.split(new char[] {'\\'});
string Answer = "";

for(int x = 0; x < myChildren.length; x++ )
{
if (myChildren[x].length 8)
myChildren[x] = myChildren[x].substring(0,6) + @"~1";
Answer += myChildren[x] + "\\";
}
return Answer;
}

I didnt run this through the compliler yet....
Hope the fixes you prob.
"CapCity" wrote:
If I have a strig that represents a path, how can I programmatically get the
"short" DOS name for it?

For example, if I have "C:\Program Files\" I want "C:\Progra~1\".

Thanks
Feb 1 '07 #3
TheMadHatter,

Man, don't get me wrong. but sometimes we have ~2, ~3, numbers, etc. I don't
think that would work for everything.

I've found a pretty interesting article on the internet.
http://www.c-sharpcorner.com/UploadF...d-8d2348b8c6d2

--
Regards,
Robson Siqueira
Enterprise Architect
"TheMadHatter" <Th**********@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
Cheat :)

public string FakeDosFileString(string myStr)
string[] myChildren = myStr.split(new char[] {'\\'});
string Answer = "";

for(int x = 0; x < myChildren.length; x++ )
{
if (myChildren[x].length 8)
myChildren[x] = myChildren[x].substring(0,6) + @"~1";
Answer += myChildren[x] + "\\";
}
return Answer;
}

I didnt run this through the compliler yet....
Hope the fixes you prob.
"CapCity" wrote:
>If I have a strig that represents a path, how can I programmatically get
the
"short" DOS name for it?

For example, if I have "C:\Program Files\" I want "C:\Progra~1\".

Thanks

Feb 1 '07 #4
public string FakeDosFileString(string myStr)
string[] myChildren = myStr.split(new char[] {'\\'});
string Answer = "";

for(int x = 0; x < myChildren.length; x++ )
{
if (myChildren[x].length 8)
myChildren[x] = myChildren[x].substring(0,6) + @"~1";
Answer += myChildren[x] + "\\";
}
return Answer;
}

I didnt run this through the compliler yet....
Hope the fixes you prob.
You can't assume the tilde character will be in use but in any case, it's
always safer to rely on the official version of any function.
Feb 1 '07 #5
True, very true, hence why I called it a cheat.

The conditions of the program I wrote was a string input, returning a string
output.
Under that condition, it should work *cough* correctly.
(except the fact that I forgot one "{" )

"Robson Siqueira" wrote:
TheMadHatter,

Man, don't get me wrong. but sometimes we have ~2, ~3, numbers, etc. I don't
think that would work for everything.

I've found a pretty interesting article on the internet.
http://www.c-sharpcorner.com/UploadF...d-8d2348b8c6d2

--
Regards,
Robson Siqueira
Enterprise Architect
"TheMadHatter" <Th**********@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
Cheat :)

public string FakeDosFileString(string myStr)
string[] myChildren = myStr.split(new char[] {'\\'});
string Answer = "";

for(int x = 0; x < myChildren.length; x++ )
{
if (myChildren[x].length 8)
myChildren[x] = myChildren[x].substring(0,6) + @"~1";
Answer += myChildren[x] + "\\";
}
return Answer;
}

I didnt run this through the compliler yet....
Hope the fixes you prob.
"CapCity" wrote:
If I have a strig that represents a path, how can I programmatically get
the
"short" DOS name for it?

For example, if I have "C:\Program Files\" I want "C:\Progra~1\".

Thanks


Feb 1 '07 #6
Probably the best idea would be to use Robson's idea,
except for one modification:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern uint GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszLongPath,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder lpszShortPath,
uint cchBuffer);

That way you can get the short name instead of the other way around.
*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)

"Larry Smith" wrote:
public string FakeDosFileString(string myStr)
string[] myChildren = myStr.split(new char[] {'\\'});
string Answer = "";

for(int x = 0; x < myChildren.length; x++ )
{
if (myChildren[x].length 8)
myChildren[x] = myChildren[x].substring(0,6) + @"~1";
Answer += myChildren[x] + "\\";
}
return Answer;
}

I didnt run this through the compliler yet....
Hope the fixes you prob.

You can't assume the tilde character will be in use but in any case, it's
always safer to rely on the official version of any function.
Feb 1 '07 #7
That way you can get the short name instead of the other way around.
*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)
I think I was already first out of the block to point that out. However, the
WinAPI version requires that the file already exists on disk (stupid).
ERROR_FILE_NOT_FOUND will be returned otherwise. I wrote a (C++) wrapper a
number of years ago that provides an option to create a temporary file first
if the file doesn't already exist (which is still problemati if you don't
have write access to the folder). "GetShortPathName()" will then succeed and
you can kill the temporary. A hack of course but what more can you do.
Feb 1 '07 #8
Probably the real q is why one would want the old dos way anyhow.

Why would you go to the trouble of getting the dos format
of a file and directory that doesnt exist?
"Larry Smith" wrote:
That way you can get the short name instead of the other way around.
*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)

I think I was already first out of the block to point that out. However, the
WinAPI version requires that the file already exists on disk (stupid).
ERROR_FILE_NOT_FOUND will be returned otherwise. I wrote a (C++) wrapper a
number of years ago that provides an option to create a temporary file first
if the file doesn't already exist (which is still problemati if you don't
have write access to the folder). "GetShortPathName()" will then succeed and
you can kill the temporary. A hack of course but what more can you do.
Feb 1 '07 #9
Probably the real q is why one would want the old dos way anyhow.

Because the format's still supported on Windows and there are countless
legacy applications that still deal with it (try chaniging directories from
the command prompt using a short name for instance and watch what happens to
the prompt). I just recently left a very large and well-known company for
instance and they're using a major package from another large and well-known
company that still has 8.3 issues which I had to deal with on occasion. The
format's still out there IOW but unless you're a grizzled programming vet
from the 80s and 90s (since they're the ones with the know-how to deal with
the older technology), you probably won't see it much. I'm one of the older
ones who does from time-to-time.
Why would you go to the trouble of getting the dos format
of a file and directory that doesnt exist?
For the same reasons you'ld retrieve the file name for any file that doesn't
exist. A file's a file regardless of its naming convention (8.3 or not) so
you may have to deal with it even before it exists (if only to check its
existence, create it from scrach, or whatever).
Feb 2 '07 #10

"Larry Smith" <no_spam@_nospam.comwrote in message
news:uj**************@TK2MSFTNGP05.phx.gbl...
>That way you can get the short name instead of the other way around.
*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)

I think I was already first out of the block to point that out. However,
the WinAPI version requires that the file already exists on disk (stupid).
No, it's quite right. If the file doesn't exist, then ERROR_FILE_NOT_FOUND
(or some other error code) is appropriate, because the question can't be
answered.
ERROR_FILE_NOT_FOUND will be returned otherwise. I wrote a (C++) wrapper a
number of years ago that provides an option to create a temporary file
first if the file doesn't already exist (which is still problemati if you
don't have write access to the folder). "GetShortPathName()" will then
succeed and you can kill the temporary. A hack of course but what more can
you do.
But that doesn't make sense. The value of the 8.3 name depends on what
files already exist in the directory. "Program Files" usually turns into
PROGRA~1, but if there's already, say, a "Program Loader", it would be
PROGRA~1, and "Program Files" would become PROGRA~2. There's no guarantee
that your temporary file would have the same 8.3 name as another file of the
same long name that gets created at another time. (What would be safe is to
create the temp file with some marker that says it's OK for the real file to
overwrite it. Not necessarily possible with a third-party app, I know)

Feb 2 '07 #11
>>That way you can get the short name instead of the other way around.
>>*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)

I think I was already first out of the block to point that out. However,
the WinAPI version requires that the file already exists on disk
(stupid).

No, it's quite right. If the file doesn't exist, then
ERROR_FILE_NOT_FOUND (or some other error code) is appropriate, because
the question can't be answered.
>ERROR_FILE_NOT_FOUND will be returned otherwise. I wrote a (C++) wrapper
a number of years ago that provides an option to create a temporary file
first if the file doesn't already exist (which is still problemati if you
don't have write access to the folder). "GetShortPathName()" will then
succeed and you can kill the temporary. A hack of course but what more
can you do.

But that doesn't make sense. The value of the 8.3 name depends on what
files already exist in the directory. "Program Files" usually turns into
PROGRA~1, but if there's already, say, a "Program Loader", it would be
PROGRA~1, and "Program Files" would become PROGRA~2. There's no guarantee
that your temporary file would have the same 8.3 name as another file of
the same long name that gets created at another time. (What would be safe
is to create the temp file with some marker that says it's OK for the real
file to overwrite it. Not necessarily possible with a third-party app, I
know)
You're missing the point. I simply want to pass "C:\Program Files\Whatever"
to a function that will return "C:\Progra~2\Whatever" even though "Whatever"
doesn't exist yet. "GetShortPathName()" will therefore fail so the
work-around is to give it what it wants. Create "C:\Program Files\Whatever"
first, pass it to "GetShortPathName()" and then remove it. It's that simple.
The real issue is that the OS can give me the short name at the time I ask
for it even though it doesn't physically exist yet. Just tell me what the
short name would be if I created it right now. What I do with it afterwards
is not its concern.
Feb 2 '07 #12

"Larry Smith" <no_spam@_nospam.comwrote in message
news:uc***************@TK2MSFTNGP06.phx.gbl...
>>>That way you can get the short name instead of the other way around.
*Sigh* Where would the world be with out Pinvokes. (www.pinvoke.net)

I think I was already first out of the block to point that out. However,
the WinAPI version requires that the file already exists on disk
(stupid).

No, it's quite right. If the file doesn't exist, then
ERROR_FILE_NOT_FOUND (or some other error code) is appropriate, because
the question can't be answered.
>>ERROR_FILE_NOT_FOUND will be returned otherwise. I wrote a (C++) wrapper
a number of years ago that provides an option to create a temporary file
first if the file doesn't already exist (which is still problemati if
you don't have write access to the folder). "GetShortPathName()" will
then succeed and you can kill the temporary. A hack of course but what
more can you do.

But that doesn't make sense. The value of the 8.3 name depends on what
files already exist in the directory. "Program Files" usually turns into
PROGRA~1, but if there's already, say, a "Program Loader", it would be
PROGRA~1, and "Program Files" would become PROGRA~2. There's no guarantee
that your temporary file would have the same 8.3 name as another file of
the same long name that gets created at another time. (What would be
safe is to create the temp file with some marker that says it's OK for
the real file to overwrite it. Not necessarily possible with a
third-party app, I know)

You're missing the point. I simply want to pass "C:\Program
Files\Whatever" to a function that will return "C:\Progra~2\Whatever" even
though "Whatever" doesn't exist yet. "GetShortPathName()" will therefore
fail so the work-around is to give it what it wants. Create "C:\Program
Files\Whatever" first, pass it to "GetShortPathName()" and then remove it.
It's that simple.
It's simpler still to do a bit of parsing and chop off anything past the
last segment that needs conversion. The OS is happy to tell you the short
name for "C:\Program Files", and there's no need for you to create a temp
file or worry about read-only directories.
Feb 2 '07 #13
It's simpler still to do a bit of parsing and chop off anything past the
last segment that needs conversion. The OS is happy to tell you the short
name for "C:\Program Files", and there's no need for you to create a temp
file or worry about read-only directories.
It shouldn't be necessary to muck around with this stuff if
"GetShortPathName()" was doing its job correctly. That's the point.
Moreover, you're not dealing with a simple parse. You need to deal with
paths that may physically exist up to the nth subfolder only (requiring an
iterative check of some sort), paths that aren't fully-qualified, etc. The
tilde character is also not guaranteed on all systems and MSFT states that
you shouldn't rely on it as I recall (though it's been a long time).
Guessing exactly how the shortname is created under all circumstances is
also unreliable. I may be naive to assume MSFT's algorithm always follows
the accepted convention of "AAAAAA~#". It might not and/or there may be
issues you and I aren't aware of (noting that file names have elaborate
rules that may impact the outcome). It's simply safer and easier to rely on
the OS.
Feb 2 '07 #14

"Larry Smith" <no_spam@_nospam.comwrote in message
news:uH**************@TK2MSFTNGP06.phx.gbl...
>It's simpler still to do a bit of parsing and chop off anything past the
last segment that needs conversion. The OS is happy to tell you the
short name for "C:\Program Files", and there's no need for you to create
a temp file or worry about read-only directories.

It shouldn't be necessary to muck around with this stuff if
"GetShortPathName()" was doing its job correctly.
But it is. It's telling you the correct name when it's known, and refusing
to do so when it can't be calculated.
That's the point. Moreover, you're not dealing with a simple parse.
Sure you are. Components in the path are separated by either "/" or "\",
and the legal 8.3 characters are well-defined. Remove everything after the
last component that needs to be translated. If you don't get an answer from
that, it's because the answer isn't known.

In other words, if you want to translate C:\Program Files\a\b\c\d\e, ask for
C:\Program Files and tack a\b\c\d\e onto the answer. If you don't get an
answer, C:\Program Files doesn't exist, and you can't really be sure what
its 8.3 name will be when it's created.
You need to deal with paths that may physically exist up to the nth
subfolder only (requiring an iterative check of some sort),
The OS call does that check for you :-)
paths that aren't fully-qualified, etc.
I don't see why that matters.
The tilde character is also not guaranteed on all systems and MSFT states
that you shouldn't rely on it as I recall (though it's been a long time).
If I were MSFT, I'd say the same thing. No point making guarantees you
might have to break.
Guessing exactly how the shortname is created under all circumstances is
also unreliable. I may be naive to assume MSFT's algorithm always follows
the accepted convention of "AAAAAA~#". It might not and/or there may be
issues you and I aren't aware of (noting that file names have elaborate
rules that may impact the outcome). It's simply safer and easier to rely
on the OS.
Agreed. I'm not proposing you do otherwise. The only assumptions you need
to make are about the use of ":", "/", and "\", and that should be safe.
Feb 2 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: bearophileHUGS | last post by:
I have a class Surface with many methods. Working in the interactive window I receive an error like this when I write the wrong method name: >>> table.addGlas() Traceback (most recent call...
5
by: Frank Borell | last post by:
I'm having a problem retrieving the ShortName from a file that was written by a MAC and only from a Windows 2003 server. I realize the file has a space after it, but I can retrieve the short...
46
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
1
by: Bruce Wood | last post by:
I'm having a devil of a time calling DeviceCapabilities() in order to get the list of paper names / codes / sizes for a printer. Here is my code and the input it produces: static extern Int32...
2
by: Thomas Müller | last post by:
Hi, I need a way to find the names of the computers that are reachable over a network. Ideally when it is executed it should report back wich computernames are present. I have seen one...
4
by: Ben | last post by:
Hi We have a list of file paths and short file names in our database. We need to loop through them all, picking up the long file name from the file itself and check that the long file name...
1
by: Rups | last post by:
API FindFirstFile searches for files based on long and short names(8+3). If i need to search files based on long names only . How can i do ?Is there any other API which does search based on long...
3
by: engteng | last post by:
How to display OS Name in short Name eg Win Vista SP1 Win XP 2003 SP2 Regards, Tee ET
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.