VB脚本可以更改或删除特殊字符

时间:2015-03-02 10:47:10

标签: vbscript

我有一个小的VB脚本(见下文),我通过谷歌找到它,它的作用是在一个XML文件中找到一个这个字符串(H * 699557 / 1A)并将文件名重命名为该字符串,这非常有效。它会遇到一个特殊字符(如字符串示例中所示)然后停止。

有人可以帮我删除特殊字符,任何帮助表示赞赏。

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\vbs"
Set objFolder = objFS.GetFolder(strFolder)

Set regEx = New RegExp  
regEx.Pattern = ".*<SuppliersInvoiceNumber>(.*?)</SuppliersInvoiceNumber>.*"
regEx.IgnoreCase = True 
regEx.MultiLine = True

For Each strFile In objFolder.Files
    strFileName = strFile.Name 
    If InStr(strFileName ,"USI") > 0 Then
        Set objFile=objFS.OpenTextFile(strFileName)
        strData = objFile.ReadAll 
        Set objFile=Nothing
        Set colMatches = regEx.Execute(strData)
        For Each objMatch In colMatches
           strNew = Split(objMatch.Submatches(0),"\")
           strNewFile = strNew(0)
           strFile.Name = strNewFile & ".xml"
        Next
    End If
Next

1 个答案:

答案 0 :(得分:0)

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\vbs"
Set objFolder = objFS.GetFolder(strFolder)

Set regEx = New RegExp  
regEx.Pattern = ".*<SuppliersInvoiceNumber>(.*?)</SuppliersInvoiceNumber>.*"
regEx.IgnoreCase = True 
regEx.MultiLine = True

For Each strFile In objFolder.Files
    strFileName = strFile.Name 
    If InStr(strFileName ,"USI") > 0 Then
        Set objFile=objFS.OpenTextFile(strFileName)
        strData = objFile.ReadAll 
        Set objFile=Nothing
        Set colMatches = regEx.Execute(strData)
        For Each objMatch In colMatches
           strNew = Split(objMatch.Submatches(0),"\")
           strNewFile = strNew(0)
           strFile.Name = Replace(Replace(strNewFile,"*",""),"/","")  & ".xml"
        Next
    End If
Next