Pregunta

Los siguientes códigos newLISP me muestra los atributos de archivo de los archivos en Win32. Sin embargo, algunos de los nombres de los archivos recuperados tienen caracteres chinos en el nombre. Cuando la función GetFileAttributesA se encuentra con ellas, me da un -1 para el atributo. Miré a GetFileAttributesW, pero no saben cómo hacer que el contenido de la fnombre a disposición de la función en una forma que reconoce.

¿Cómo se puede manejar esta situación? (Estoy dispuesto a considerar probar otro idioma)

(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")
¿Fue útil?

Solución 3

En aras de la exhaustividad, aquí está la solución, que se encuentra en consulta con la gente en la newLISP foro .

No haber reemplazado la técnica bits slice reverse en el bit de atributo con el operador & más apropiado. Lo que queda para el lector.

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

Otros consejos

Desde la versión 10.3.2, publicada en julio de 20mo de 2011, 10.3.2, MultiByteToWideChar es manejado internamente por newLISP al leer los nombres de ruta / archivo.

Tal vez usted no utilice versión Unicode de newLISP. Anycase, hay muy pocos Newlispers aquí. Prueba a newLISP foro lugar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top