1

下面的 NewLISP 代码显示了 Win32 下文件的文件属性。但是,某些检索到的文件名中包含中文字符。当 GetFileAttributesA 函数遇到它们时,它会给我一个属性 -1。我查看了 GetFileAttributesW,但不知道如何以它识别的形式使 fname 的内容对函数可用。

如何处理这种情况?(我愿意考虑尝试另一种语言)

(define (get-archive-flag file-name)
    (if (not GetFileAttributesA)
        (begin
        (import "kernel32.DLL" "GetFileAttributesA")
        )
    )
    (setq fname file-name file-attrib (GetFileAttributesA (address fname)))   
    (append fname " " ( string file-attrib))    
)

; walks a disk directory and prints all path-file names
;
(define (show-tree dir)
    (if (directory dir)
        (dolist (nde (directory dir))
            (if (and (directory? (append dir "/" nde))
                (!= nde ".") (!= nde ".."))
                (show-tree (append dir "/" nde))
                (println (get-archive-flag (append dir "/" nde)))
            )
        )
    )
)

(show-tree "z:\\working files\\Cathy")
4

3 回答 3

2

自 10.3.2 版(于 2011 年 7 月 20 日 10.3.2 发布)以来,MultiByteToWideChar 在读取路径/文件名时由 newLISP 内部处理。

于 2011-10-01T15:30:31.160 回答
1

也许你不使用 Unicode 版本的 Newlisp。无论如何,这里很少有Newlispers。请尝试Newlisp 论坛

于 2010-06-21T20:08:05.173 回答
1

为了完整起见,这是在与NewLISP 论坛上的人们协商后找到的解决方案。

我没有用bits slice reverse更合适的运算符替换属性位上的技术&。这是留给读者的。

(constant 'SIZEOF_WCHAR 2) ; assumption
(constant 'CP_UTF8 65001)

(define (utf8->16 lpMultiByteStr , cchWideChar lpWideCharStr ret)
    (if (not MultiByteToWideChar)
        (begin
        (import "kernel32.DLL" "MultiByteToWideChar")
        )
    )
    ; calculate the size of buffer (in WCHAR's)
    (setq cchWideChar 
        (
        MultiByteToWideChar
        CP_UTF8 ; from UTF-8
        0       ; no flags necessary
        lpMultiByteStr
        -1      ; convert until NULL is encountered
        0
        0
        )
    )

    ; allocate the buffer
    (setq lpWideCharStr (dup " " (* cchWideChar SIZEOF_WCHAR)))

    ; convert
    (setq ret 
        (
        MultiByteToWideChar
        CP_UTF8 ; from UTF-8
        0       ; no flags necessary
        lpMultiByteStr
        -1      ; convert until NULL is encountered
        lpWideCharStr
        cchWideChar
        )
    )
    (if (> ret 0) lpWideCharStr nil)
)

; resets the Win32 archive flag on a file
; By CaveGuy 2009

(define (get-archive-flag file-name)
    (if (not GetFileAttributesW)
        (begin
        (import "kernel32.DLL" "GetFileAttributesW")
        )
    )
    (setq fname file-name
        file-attrib (GetFileAttributesW (utf8->16 fname))
    )   
    file-attrib   
)

; walks a disk directory and prints all path-file names where archive bit is set
;
(define (show-tree dir)
    (if (directory dir)
        (dolist (nde (directory dir))
            (if (and (directory? (append dir "/" nde)) (!= nde ".") (!= nde "..") )
                (show-tree (append dir "/" nde))
                (if (not (or (= nde ".") (= nde "..")))
                    (begin
                    (setq fname (append dir "/" nde))
                    (setq fflag (get-archive-flag fname))
                    (setq fbits (bits fflag))
                    (if (= (slice (reverse fbits) 5 1) "1") (println fname))
                    )
                )
            )
        )
    )
)

(show-tree "//server/folder")
于 2010-07-07T03:53:46.060 回答