使用NewLISP查找中文文件名的属性?

发布于 2024-09-06 17:38:24 字数 925 浏览 2 评论 0原文

下面的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")

The following NewLISP code shows me the file attributes of files under Win32. However, some of the filenames retrieved have Chinese characters in the name. When the GetFileAttributesA function encounters them, it gives me a -1 for the attribute. I looked at GetFileAttributesW but don't know how to make the contents of the fname available to the function in a form it recognises.

How does one handle this situation? (I am willing to consider trying another language)

(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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2024-09-13 17:38:24

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

Since version 10.3.2, relased July 20th 2011, 10.3.2 , MultiByteToWideChar is handled internally by newLISP when reading path/file names.

雪花飘飘的天空 2024-09-13 17:38:24

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

Maybe you do not use Unicode version of Newlisp. Anycase, there are very few Newlispers here. Try Newlisp forum instead.

迷爱 2024-09-13 17:38:24

为了完整起见,这是与 NewLISP 论坛

我还没有用更合适的 & 运算符替换属性位上的位切片反转技术。这就留给读者了。

(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")

For the sake of completeness, here's the solution, found in consultation with the folk on the NewLISP forum.

I haven't replaced the bits slice reverse technique on the attribute bit with the more appropriate & operator. That's left for the reader.

(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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文