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

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'