VBS删除Readonly属性recursivelty

时间:2013-03-04 15:14:36

标签: recursion vbscript attributes wsh

我对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

1 个答案:

答案 0 :(得分: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