I have a directory with filenames that contain “funny” characters. I'd like to get the dos (8.3) filename for the files. How can I do that? My language of choice is Go.
C:\...\foo>dir /x
Volume in Laufwerk C: hat keine Bezeichnung.
Volumeseriennummer: 329F-C2FE
Verzeichnis von C:\...\foo
29.09.2018 16:22 <DIR> .
29.09.2018 16:22 <DIR> ..
23.07.2009 01:52 5.526 CW9463~1.PDF cöw.pdf
1 Datei(en), 1.922.706 Bytes
0 Verzeichnis(se), 48.235.646.976 Bytes frei
I'd like to get the CW9463~1.PDF
filename instead of cöw.pdf
.
(I will use this filename to open a file for reading.)
I have found a solution, but it doesn't really help me. (See the comment/warning from @eryksun)
import (
"golang.org/x/sys/windows"
)
func GetShortPathName(name string) (path string, err error) {
lp, err := windows.UTF16PtrFromString(name)
if err != nil {
return "", err
}
n := uint32(100)
for {
buf := make([]uint16, n)
n, err = windows.GetShortPathName(lp, &buf[0], uint32(len(buf)))
if err != nil {
return "", err
}
if n <= uint32(len(buf)) {
return windows.UTF16ToString(buf[:n]), nil
}
}
}