2015-03-02 151 views
1

我有一个小的VB脚本(见下文),我通过谷歌发现,它所做的是在XML文件中找到一个此字符串(H * 699557/1A),并重命名文件名到该字符串,这工作出色,直到遇到一个特殊的字符(如字符串示例),然后停止。VB脚本来更改或删除特殊字符

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

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 
+0

我不明白的问题。你能举一个它停止的例子吗?哪些特殊字符导致它失败?你能发布错误信息吗? – 2015-03-02 20:28:40

+0

嗨Nathan,我正在使用它来更改文件名作为这些“* /”字符的字符串,我需要删除它们之前重命名该文件。错误是:行:18,字符6,错误:无效的过程调用或参数,代码:800A0005,来源:Microsoft VBScript运行时错误。 – DanDare 2015-03-03 09:57:02

+0

在上面的代码中哪些行号对应? – 2015-03-03 09:58:41

回答

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 
+1

作品完美,感谢您的帮助。 – DanDare 2015-03-03 10:18:49

+1

对不起,内森,新手到这个网站,我现在已经接受了答案,但由于在这里新来无法upvote。但你的帮助非常感谢。谢谢 – DanDare 2015-03-03 10:43:55

+1

当我获得15岁以上的声望时,我会回来并且upvote。 Cheers – DanDare 2015-03-03 10:53:53