删除文件名vbscript中的数字和句点

时间:2013-12-16 22:31:29

标签: vbscript filenames

我已经找到了一个脚本,它将替换下划线(_)和我在脚本中编写的其他文本。我需要修改这个脚本,这样它也会删除文件名中的所有数字。我尝试了[0-9]和/ d,但没有删除文件名中的数字。此外,我尝试删除文件名中的句点,但也删除了文件扩展名。所以它也带走了.csv。有人可以帮忙吗?

'========================================================
' VBScript to replace underscore in file name with space 
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' get current folder
Set fol = fso.GetFolder(".")

' go thru each files in the folder
For Each fil In fol.Files
    ' check if the file name contains underscore
    If InStr(1, fil.Name, "_") <> 0 Then
        ' replace underscore with space
        sName = Replace(fil.Name, "_", " ")
        ' rename the file
        fil.Name = sName
    End If
Next

' echo the job is completed
WScript.Echo "Completed!"

1 个答案:

答案 0 :(得分:2)

Replace函数只进行简单的字符串替换。它不支持通配符或模式。您正在寻找regular expressions

Set re = New RegExp
re.Pattern = "[0-9_.]"
re.Global  = True

For Each fil In fol.Files
  fil.Name = re.Replace(fil.Name, " ")
Next