0

我创建了一个 VB 脚本以递归方式列出其所有文件和子文件夹文件。该脚本开始正常,但最终在包含文件名中包含不可打印字符的文件的任何文件夹中崩溃,即当我在资源管理器中浏览文件夹时看到小方块。我不确定如何更改以下错误处理以在找到具有此类字符的文件时继续。

任何建议或解决方案将不胜感激。谢谢你。

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
4

1 回答 1

1

将输出文本文件创建为 unicode,以便它可以处理“不可打印”字符。CreateTextFile 的第三个参数。

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

已编辑

如果您不能使用 unicode 文件,则应在写入输出文件之前将文件/文件夹名称从 unicode 转换为 ansi。这将进行转换

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

并修改代码来调用它NewFile.WriteLine Unicode2Ansi(objFile.Path)

于 2013-11-12T10:05:30.573 回答