Change long path name to short pathname 
Author Message
 Change long path name to short pathname

Hi all

I know that it is possible but right now I don't know how. I suppose it can
be done with a API call?

What is the problem. Normally the application will be installed in
'C:\Program Files'. I use a shell command to start a executable and with a
document. I get an error when the pathname contains a space. When I chance
'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space and
replace it by '~1' is sometimes tricky, so is there a function that gives me
back de short (DOS) name.

Thanks in advance

Tim



Sun, 14 Sep 2003 22:11:36 GMT  
 Change long path name to short pathname
Tim

There is a function for this

Public Declare Function GetShortPathName Lib "kernel32" _
    Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

Function ShortPath(strLongPath As String) As String
Dim ret As Long
ShortPath = Space$(128)
ret = GetShortPathName(strLongPath, ShortPath, Len(ShortPath))
ShortPath = Left$(ShortPath, ret)
End Function

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Hi all

> I know that it is possible but right now I don't know how. I suppose it
can
> be done with a API call?

> What is the problem. Normally the application will be installed in
> 'C:\Program Files'. I use a shell command to start a executable and with a
> document. I get an error when the pathname contains a space. When I chance
> 'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space and
> replace it by '~1' is sometimes tricky, so is there a function that gives
me
> back de short (DOS) name.

> Thanks in advance

> Tim



Sun, 14 Sep 2003 22:27:48 GMT  
 Change long path name to short pathname
The following is a compilation of several posts I've given in the past
regarding the Shell command. It gives you more information than you asked
about. Somewhere in there is the answer to your question -- the rest is for
your future consideration.

Rick

You can use the Shell command. To execute internal DOS command (Dir, Copy,
etc. as well as redirection of screen output), the command processor must be
specified (using the Environ function and "comspec" as its argument returns
the correct command processor path on NT and non-NT systems) . Specifying
the command processor is safe & generic and will work with non-internal
commands also. That syntax, using an XCopy command as an example is:

Shell  Environ("comspec") & " /c xcopy """ & _
         Source & """ """ & Destination & """ " & Option, vbHide

You set the Source and Desination (string variables) to the appropriate
paths and the Option (string variable), if any, which can be found by
opening an MSDOS Prompt window and typing xcopy /?. (Note: You can type /?
after any DOS command at a DOS prompt to list the available options for that
command.) One more example would be to list all the files in a directory
including subdirectories and subdirectories of subdirectories and all of
their files.

  CommandLine = "dir """ & FileSpec & _
                             """ /s/b > """ & RedirectTo & """"
  Shell Environ("comspec") & " /c " & CommandLine, vbHide

Here, the output of a Dir command is redirected to a file-path you specify
in the RedirectTo (string variable). The /s/b are options to the Dir command
that tell it to recurse throught its subdirectories and not to include
header or summary information.

I used a variable for the file name so that I could more easily explain the
benefit of encasing it in quotemarks. If you redirect to a file that has
spaces in its name, or if there are spaces in the path specification itself,
then the filename *must* be quoted to protect the spaces from DOS's desire
to use them as delimiters. (That's what all those quotemarks in the Shell
statement are for.) If the filename doesn't have spaces in it, the quotes
aren't necessary BUT they don't hurt either. Hence, the above will work with
either.

As for your PING question, something like the following should work:

     strIP = "4.17.23.1"
     Shell Environ("comspec") & " /c ping " & _
              strIP & " > """ & RedirectFile & """", vbHide

Although you didn't specify it in your original post, I assume you want to
use vbHide for the optional 2nd parameter to Shell. This hides the DOS
window so that your user doesn't see it. If you want the DOS window to
remain visible, you would use the vbNormalFocus BUT you must use a /k
instead of a /c for the command processor argument. Basically, the /c tells
the command processor "here comes a command and, when its finished
executing, close the DOS shell it is running in" whereas the /k also tells
the command processor that a command follows, but it instructs it to leave
the DOS session running.

The above assumes you do NOT have to wait for this file to be completely
written before your code continues executing. If you have to work with this
file right after it is created, consider one of these (which makes your
program wait until the DOS process is finished):

MICROSOFT'S OFFICIAL WAY
========================
See this link

http://support.microsoft.com/support/kb/articles/Q129/7/96.asp

Note: This method doesn't use Shell -- it uses CreateProcessA.

FAST AND DIRTY METHOD (WORKS ALMOST ALL THE TIME)
=================================================
Paste these lines in the (General)(Declarations) section of the form where
the Shell is being called (or remove the Private keywords and put them in a
BAS module if more than one form will use them):

Private Declare Function OpenProcess _
        Lib "kernel32" _
        (ByVal dwDesiredAccess As Long, _
         ByVal bInheritHandle As Long, _
         ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle _
        Lib "kernel32" _
        (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject _
        Lib "kernel32" _
        (ByVal hHandle As Long, _
         ByVal dwMilliseconds As Long) As Long

Call your Shell command in this form with the appropriate Shell arguments
placed in the parentheses:

PID = Shell( <<Put Shell Arguments Here>> )

And finally, paste the following IMMEDIATELY after the PID=Shell statement
above (making sure to handle the possible error where indicated; i.e. stop
the code from falling through to your other commands if the Shell failed):

If PID = 0 Then
     '
     'Handle Error, Shell Didn't Work
     '
Else
     hProcess = OpenProcess(&H100000, True, PID)
     WaitForSingleObject hProcess, -1
     CloseHandle hProcess
End If

One note -- there are some NT systems (those with VERY tight security
measures in place) where this call won't work. No problem at all on 95/98
though.

Rick Rothstein


Quote:
> Hi all

> I know that it is possible but right now I don't know how. I suppose it
can
> be done with a API call?

> What is the problem. Normally the application will be installed in
> 'C:\Program Files'. I use a shell command to start a executable and with a
> document. I get an error when the pathname contains a space. When I chance
> 'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space and
> replace it by '~1' is sometimes tricky, so is there a function that gives
me
> back de short (DOS) name.

> Thanks in advance

> Tim



Sun, 14 Sep 2003 23:01:04 GMT  
 Change long path name to short pathname
Thank Ian, it works.

Tim



Quote:
> Tim

> There is a function for this

> Public Declare Function GetShortPathName Lib "kernel32" _
>     Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
>     ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

> Function ShortPath(strLongPath As String) As String
> Dim ret As Long
> ShortPath = Space$(128)
> ret = GetShortPathName(strLongPath, ShortPath, Len(ShortPath))
> ShortPath = Left$(ShortPath, ret)
> End Function

> regards

> Ian

> ** invalid email address, change dk to denmark

> homepage http://www.kingsoft-denmark.com/
> Tips & Tricks page http://tips.kingsoft-denmark.com/



> > Hi all

> > I know that it is possible but right now I don't know how. I suppose it
> can
> > be done with a API call?

> > What is the problem. Normally the application will be installed in
> > 'C:\Program Files'. I use a shell command to start a executable and with
a
> > document. I get an error when the pathname contains a space. When I
chance
> > 'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space
and
> > replace it by '~1' is sometimes tricky, so is there a function that
gives
> me
> > back de short (DOS) name.

> > Thanks in advance

> > Tim



Mon, 15 Sep 2003 01:53:30 GMT  
 Change long path name to short pathname
Also try "My File Name.Dat"

note the Chr$(34)s around the name



Quote:
>Thank Ian, it works.

>Tim



>> Tim

>> There is a function for this

>> Public Declare Function GetShortPathName Lib "kernel32" _
>>     Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
>>     ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

>> Function ShortPath(strLongPath As String) As String
>> Dim ret As Long
>> ShortPath = Space$(128)
>> ret = GetShortPathName(strLongPath, ShortPath, Len(ShortPath))
>> ShortPath = Left$(ShortPath, ret)
>> End Function

>> regards

>> Ian

>> ** invalid email address, change dk to denmark

>> homepage http://www.kingsoft-denmark.com/
>> Tips & Tricks page http://tips.kingsoft-denmark.com/



>> > Hi all

>> > I know that it is possible but right now I don't know how. I suppose it
>> can
>> > be done with a API call?

>> > What is the problem. Normally the application will be installed in
>> > 'C:\Program Files'. I use a shell command to start a executable and with
>a
>> > document. I get an error when the pathname contains a space. When I
>chance
>> > 'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space
>and
>> > replace it by '~1' is sometimes tricky, so is there a function that
>gives
>> me
>> > back de short (DOS) name.

>> > Thanks in advance

>> > Tim



Mon, 15 Sep 2003 05:39:24 GMT  
 Change long path name to short pathname
Several responses have shown how to convert a long file name to a short one.
If you want to go the other way, short to long, see Microsoft Knowledge
Base: Q154822 (get a long filename from a short filename). Q175512 deals
with the issue you raised (get a short filename from a long filename).


Quote:
> Hi all

> I know that it is possible but right now I don't know how. I suppose it
can
> be done with a API call?

> What is the problem. Normally the application will be installed in
> 'C:\Program Files'. I use a shell command to start a executable and with a
> document. I get an error when the pathname contains a space. When I chance
> 'C:\Prograg Files' to 'C:\Program~1' it works fine. Search for a space and
> replace it by '~1' is sometimes tricky, so is there a function that gives
me
> back de short (DOS) name.

> Thanks in advance

> Tim



Mon, 15 Sep 2003 07:59:39 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. From Short Pathnames to Long Pathnames?

2. Changing Long Paths to Short Paths in a 16-bit App

3. Getting Short Path Names from Long

4. Getting Short Path Names from Long

5. Getting Short Path Names from Long

6. Long to Short file name change, HELP!

7. App.Path only returning short path name - Why?

8. app.path only returns short path name

9. App.Path returns "short" path name

10. Get long file name from short file name

11. Convert Long Folder Names to DOS Short Dir Names

12. Long File Names and Short File Names

 

 
Powered by phpBB® Forum Software