简体   繁体   English

文件名中的不可打印字符破坏了我的递归文件列表VB脚本

[英]Non-printable characters in file names break my recursive file listing VB Script

I created a VB script to recursively list all of its file and subfolder files. 我创建了一个VB脚本以递归方式列出其所有文件和子文件夹文件。 The script begins fine but eventually crashes in any folder containing a file with a non-printable character/s in their filenames, ie I see little squares when I browse the folder in Explorer. 该脚本可以正常运行,但最终会在文件名中包含不可打印字符的文件的任何文件夹中崩溃,即,在资源管理器中浏览该文件夹时,我会看到一个小方块。 I'm not sure how to change my below error handling to continue when it finds a file with such characters. 我不确定如何更改下面的错误处理以继续查找包含此类字符的文件。

Any advice or solutions would be appreciated. 任何建议或解决方案将不胜感激。 Thank you. 谢谢。

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\Input\"
Set objFolder = objFSO.GetFolder(strFolder)
Set NewFile = objFSO.CreateTextFile("C:\Output\" & objFolder.Name & " FileList.txt", True)
Set colFiles = objFolder.Files

On Error Resume Next

For Each objFile In colFiles
    NewFile.WriteLine(objFile.Path)
    If Err Then
        Err.Clear
    End If
Next

ShowSubFolders(objFolder)

Sub ShowSubFolders(objFolder)
    Set colFolders = objFolder.SubFolders

    For Each objSubFolder In colFolders
        Set colFiles = objSubFolder.Files
            For Each objFile In colFiles
                NewFile.WriteLine(objFile.Path)
                If Err Then
                    Err.Clear
                End If
            Next
        ShowSubFolders(objSubFolder)
    Next
End Sub

NewFile.Close

Create the output text file as unicode so it can handle "non printable" characters. 将输出文本文件创建为unicode,以便它可以处理“不可打印的”字符。 Third parameter of CreateTextFile. CreateTextFile的第三个参数。

Set NewFile = objFSO.CreateTextFile(" ... ", True, True)

EDITED 已编辑

If you can not work with unicode files, then file/folder names should be converted from unicode to ansi before writing to output file. 如果无法使用unicode文件,则在写入输出文件之前,应将文件/文件夹名称从unicode转换为ansi。 This will do the conversion 这将完成转换

Function Unicode2Ansi( text )
    Unicode2Ansi = text 
    With (WScript.CreateObject("ADODB.Stream"))
        ' Put data into stream
        .Type = 2 '( adTypeText )
        .Charset = "x-ansi"
        .Open
        .WriteText text
        'Retrieve data from stream
        .Position = 0
        Unicode2Ansi = .ReadText
        .Close
    End With 
End Function

And adapt code to call it NewFile.WriteLine Unicode2Ansi(objFile.Path) 并修改代码以将其NewFile.WriteLine Unicode2Ansi(objFile.Path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM