0

我对 VBS 很陌生,并尝试从目录中递归地删除只读属性。

它正在删除文件的只读属性,但不删除目录的属性。此外,这些目录中的文件似乎失去了相关的程序链接,现在都显示为未注册的文件类型。任何帮助是极大的赞赏。

更新:我可以看到为什么文件现在失去了关联。这是因为 . 将名称与扩展名分开的名称已被删除!嗬!理想情况下,我只想重命名文件名。

re.Pattern =  "[_.]"
re.IgnoreCase = True
re.Global = True

RemoveReadonlyRecursive("T:\Torrents\")

Sub RemoveReadonlyRecursive(DirPath)
    ReadOnly = 1
    Set oFld = FSO.GetFolder(DirPath)

    For Each oFile in oFld.Files
        If oFile.Attributes AND ReadOnly Then
            oFile.Attributes = oFile.Attributes XOR ReadOnly
        End If
        If re.Test(oFile.Name) Then
            oFile.Name = re.Replace(oFile.Name, " ")
        End If
    Next
    For Each oSubFld in oFld.SubFolders
        If oSubFld.Attributes AND ReadOnly Then
            oSubFld.Attributes = oSubFld.Attributes XOR ReadOnly
        End If
        If re.Test(oSubFld.Name) Then
            oSubFld.Name = re.Replace(oSubFld.Name, " ")
        End If

        RemoveReadonlyRecursive(oSubFld.Path)
    Next

End Sub
4

1 回答 1

3

您似乎想通过脚本自动执行可重复的操作。为什么不使用attrib命令为您执行此操作:

attrib -r "T:\Torrents\*.*" /S

如果要将其附加到可单击的图标,可以将其放在批处理文件中。

编辑:要从 VBScript 静默运行它:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "attrib -r ""T:\Torrents\*.*"" /S", 0, true)

EDIT2:要替换除最后一个句点之外的所有内容,请使用如下正则表达式:

filename = "my file.name.001.2012.extension"
Set regEx = New RegExp
' Make two captures:
' 1. Everything except the last dot
' 2. The last dot and after that everything that is not a dot
regEx.Pattern = "^(.*)(\.[^.]+)$"     ' Make two captures:

' Replace everything that is a dot in the first capture with nothing and append the second capture        
For each match in regEx.Execute(filename)
    newFileName = replace(match.submatches(0), ".", "") & match.submatches(1)
Next
于 2013-03-04T15:24:16.130 回答