PDA

Vollständige Version anzeigen : Wie ermittle ich den kurzen (DOS) bzw. den langen Namen eines Dateipfades?


Stefan Kulpa
27.01.2003, 18:42
Es kann notwendig sein, von einem langen Dateipfad den kurzen DOS-Pfad (8.3) zu ermitteln.

Aus C:\Programme\Microsoft Office\Office10\WINWORD.EXE wird C:\PROGRA~1\MICROS~2\Office10\WINWORD.EXE.

Aber auch der Weg zurück von einem kurzen zu einem langen Dateipfad wird manchmal notwendig.

Beides ist mittels des Win32-API möglich.

Die Funktion GetShortPath() ermitteln den kurzen Dateipfad und
die Funktion GetLongPath() konvertiert den kurzen Pfad zurück in die lange Variante.


<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Const</span> MAXPATH <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 260
&nbsp;
<span class="TOKEN">Type</span> FILETIME
dwLowDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwHighDateTime <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Type</span> WIN32_FIND_DATA
dwFileAttributes <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
ftCreationTime <span class="TOKEN">As</span> FILETIME
ftLastAccessTime <span class="TOKEN">As</span> FILETIME
ftLastWriteTime <span class="TOKEN">As</span> FILETIME
nFileSizeHigh <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
nFileSizeLow <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved0 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
dwReserved1 <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
cFileName <span class="TOKEN">As</span> <span class="TOKEN">String</span> * MAXPATH
cAlternate <span class="TOKEN">As</span> <span class="TOKEN">String</span> * 14
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindFirstFile <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;FindFirstFileA&quot; _
(<span class="TOKEN">ByVal</span> lpFileName <span class="TOKEN">As</span> String, _
lpFindFileData <span class="TOKEN">As</span> WIN32_FIND_DATA) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> FindClose <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(<span class="TOKEN">ByVal</span> hFindFile <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> GetShortPathName <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;GetShortPathNameA&quot; _
(<span class="TOKEN">ByVal</span> lpszLongPath <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> lpszShortPath <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> cchBuffer <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Function</span> GetShortPath(<span class="TOKEN">ByVal</span> sLongPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Methode: | Ermittelt den 8.3 (DOS)-Pfad</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Parameter: | sLongPath = g&uuml;ltiger Datei- oder Ordnerpfad</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | 8.3 (DOS)-Pfad</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="TOKEN">Dim</span> sShortPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
sShortPath = VBA.String(260, 0)
<span class="TOKEN">If</span> GetShortPathName(sLongPath, sShortPath, Len(sShortPath)) <span class="TOKEN">Then</span>
GetShortPath = _
VBA.Left(sShortPath, VBA.InStr(sShortPath, vbNullChar) - 1)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Public Function</span> GetLongPath(<span class="TOKEN">ByVal</span> sShortPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Methode: | Ermittelt den &quot;langen&quot; Pfad aus einem 8.3 (DOS)-Pfad</span>
<span class="REM">'// | ben&ouml;tigt VBA6+ wegen InStrRev-Methode</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Parameter: | sShortPath = g&uuml;ltiger 8.3 (DOS)-Pfad</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | Konvertierter Pfad (C:\PROGRA~1 -&gt; C:\Programme)</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="TOKEN">Const</span> INVALID_HANDLE_VALUE <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = -1
<span class="TOKEN">Dim</span> uWFA <span class="TOKEN">As</span> WIN32_FIND_DATA
<span class="TOKEN">Dim</span> lOffset <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> lHandle <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> sFile <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sTemp <span class="TOKEN">As</span> <span class="TOKEN">String</span>
sShortPath = VBA.Trim$(sShortPath)
<span class="TOKEN">If</span> VBA.Right$(sShortPath, 1) = &quot;\&quot; <span class="TOKEN">Then</span>
sShortPath = VBA.Left$(sShortPath, VBA.Len(sShortPath) - 1)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">Do</span> <span class="TOKEN">While</span> <span class="TOKEN">Not</span> lHandle = INVALID_HANDLE_VALUE
lHandle = FindFirstFile(sShortPath, uWFA)
sFile = VBA.Left$(uWFA.cFileName, VBA.InStr(uWFA.cFileName, vbNullChar) - 1)
<span class="TOKEN">If</span> VBA.Len(sShortPath) &gt; 2 <span class="TOKEN">Then</span>
sTemp = sFile &amp; &quot;\&quot; &amp; sTemp
lOffset = VBA.InStrRev(sShortPath, &quot;\&quot;)
<span class="TOKEN">If</span> lOffset &gt; 0 <span class="TOKEN">Then</span> sShortPath = VBA.Left$(sShortPath, lOffset - 1)
<span class="TOKEN">Else</span>
sTemp = sShortPath &amp; &quot;\&quot; &amp; sTemp
<span class="TOKEN">Exit Do</span>
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">Loop</span>
GetLongPath = VBA.Left$(sTemp, VBA.Len(sTemp) - 1)
FindClose lHandle
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Sub</span> Beispiel()
&nbsp;
<span class="TOKEN">Dim</span> sLongPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sShortPath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
sLongPath = &quot;C:\Programme\Microsoft Office\Office10\WINWORD.EXE&quot;
&nbsp;
sShortPath = GetShortPath(sLongPath)
<span class="TOKEN">Debug.Print</span> &quot;Der kurze Pfad lautet: &quot;; sShortPath
&nbsp;
sLongPath = GetLongPath(sShortPath)
<span class="TOKEN">Debug.Print</span> &quot;Der lange Pfad lautet: &quot;; sLongPath
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

Sascha Trowitzsch
16.09.2003, 00:33
Eine API-lose Variante, den langen Dateinamen zu erhalten, ist folgende:
<div><link href="http://www.ms-office-forum.net/forum/externals/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Public Function</span> GetLongName(<span class="TOKEN">ByVal</span> sShortName <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sOrdner() <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sLongName <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> i <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
<span class="TOKEN">Dim</span> sTemp <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> iCount <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
&nbsp;
GetLongName = sShortName
&nbsp;
sOrdner = Split(sShortName, &quot;\&quot;)
iCount = <span class="TOKEN">UBound</span>(sOrdner)
&nbsp;
<span class="TOKEN">On</span> <span class="TOKEN">Error</span> <span class="TOKEN">Resume</span> <span class="TOKEN">Next</span>
&nbsp;
<span class="TOKEN">For</span> i = iCount <span class="TOKEN">To</span> 1 <span class="TOKEN">Step</span> -1
Err = 0
<span class="TOKEN">If</span> i = iCount <span class="TOKEN">Then</span>
sTemp = Dir$(sShortName)
<span class="TOKEN">If</span> Err &lt;&gt; 0 <span class="TOKEN">Then</span> <span class="TOKEN">Exit For</span>
sLongName = sTemp + sLongName
<span class="TOKEN">Else</span>
sTemp = Dir$(sShortName, vbDirectory + vbHidden)
<span class="TOKEN">If</span> Err &lt;&gt; 0 <span class="TOKEN">Then</span> <span class="TOKEN">Exit For</span>
sLongName = sTemp &amp; &quot;\&quot; &amp; sLongName
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
sShortName = Left$(sShortName, Len(sShortName) - Len(sOrdner(i)) - 1)
<span class="TOKEN">Next</span> i
&nbsp;
<span class="TOKEN">If</span> Err = 0 <span class="TOKEN">Then</span> GetLongName = sOrdner(0) &amp; &quot;\&quot; &amp; sLongName
<span class="TOKEN">End</span> <span class="TOKEN">Function</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

_anton_
14.08.2008, 15:37
Hallo,

noch eine Variante(mit FileSystemObject):
<span style="font-family: Courier New,FixedSys;"><br><span style="color: #0000CC;">Sub</span> ShortPath() <br>&nbsp; <span style="color: #0000CC;">Set</span> fso = CreateObject(&quot;Scripting.FileSystemObject&quot;) &nbsp;<br>&nbsp; MsgBox fso.GetFile(&quot;C:\Programme\Microsoft Office\Office10\WINWORD.EXE&quot;).ShortPath <br><span style="color: #0000CC;">End Sub</span> &nbsp;<br><br></span>

FW
10.11.2008, 15:39
... übrigens gibt es auch das Pendant zu GetShortPathName:Declare Function GetLongPathName Lib "kernel32" Alias _
"GetLongPathNameA" _
(ByVal lpszShortPath As String, _
ByVal lpszLongPath As String, _
ByVal cchBuffer As Long) As Longso dass sich die Lösung hierfür auch einfacher gestaltet, als in #1 vorgeschlagen:Function GetLongPath(ByVal sLongPath As String) As String
'// -----------------------------------------------------------------
'// Methode: | Ermittelt den "langen" Pfad aus einem 8.3 (DOS)-Pfad
'// -----------------------------------------------------------------
'// Parameter: | sShortPath = gültiger 8.3 (DOS)-Pfad
'// -----------------------------------------------------------------
'// Rückgabe: | Konvertierter Pfad (C:\PROGRA~1 -&gt; C:\Programme)
'// -----------------------------------------------------------------
Dim sLongPath As String
sLongPath = VBA.String(260, 0)
If GetLongPathName(sShortPath, sLongPath, Len(sLongPath)) Then
GetLongPath = _
VBA.Left(sLongPath, VBA.InStr(sLongPath, vbNullChar) - 1)
End If

End Function...