drudfe0446838 2018-09-29 14:35 采纳率: 0%
浏览 181
已采纳

获取Dos(8.3)文件名

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.)

  • 写回答

1条回答 默认 最新

  • douhua1760 2018-09-30 07:55
    关注

    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
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效