VB脚本删除特定的子文件夹

时间:2012-11-17 08:13:14

标签: vbscript command-prompt

我需要VBScript / command-prompt来删除具有特定名称

的子文件夹

例如:

  1. E:\测试\ 43 \ 5512686 \ 5512698 \ HTML \ ABC
  2. E:\测试\ 43 \ 5467686 \ 5512699 \ HTML \ ABC
  3. E:\测试\ 43 \ 5587686 \ 55147589 \ HTML \ ABC
  4. E:\测试\ 45 \ 5517586 \ 5512698 \ HTML \ ABC
  5. 等,

    其中上面的例子“abc”文件夹需要删除

    任何人都可以帮忙吗

    提前致谢

2 个答案:

答案 0 :(得分:1)

有关背景/背景信息,请参阅this skeleton for recursive file access。鉴于此文件夹结构:

tree /A ..\test
Folder PATH listing for volume eh
Volume serial number is 0ED6-233C
E:\TRIALS\SOTRIALS\ANSWERS\13415663\TEST
+---vbs
\---df
    +---1
    |   +---b
    |   |   \---x
    |   \---a
    |       \---abc
    \---2
        \---abc
            \---xx

这个概念证明代码:

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

WScript.Quit Main()

Function Main()
  Dim sDir : sDir = "..\test"
  Dim oWorker : Set oWorker = New cWorker
  Main = traverseDir(goFS.GetFolder(sDir), oWorker)
End Function

Class cWorker
  Public Sub processFile(oFile)
    ' not needed
  End Sub
  Public Function processFolder(oFolder)
    WScript.Echo "looking at", oFolder.Path
    processFolder = True
    If "abc" = oFolder.Name Then
       WScript.Echo "will delete", oFolder.Path
       oFolder.Delete
       processFolder = False
    End If
  End Function
End Class

Function traverseDir(oDir, oWorker)
  traverseDir = 0
  Dim oF
  For Each oF In oDir.Files
      oWorker.processFile oF
  Next
  For Each oF In oDir.SubFolders
      If oWorker.processFolder(oF) Then
         traverseDir = traverseDir(oF, oWorker)
      End If
  Next
End Function

输出:

cscript step02.vbs
looking at E:\trials\SoTrials\answers\13415663\test\vbs
looking at E:\trials\SoTrials\answers\13415663\test\df
looking at E:\trials\SoTrials\answers\13415663\test\df\1
looking at E:\trials\SoTrials\answers\13415663\test\df\1\b
looking at E:\trials\SoTrials\answers\13415663\test\df\1\b\x
looking at E:\trials\SoTrials\answers\13415663\test\df\1\a
looking at E:\trials\SoTrials\answers\13415663\test\df\1\a\abc
will delete E:\trials\SoTrials\answers\13415663\test\df\1\a\abc
looking at E:\trials\SoTrials\answers\13415663\test\df\2
looking at E:\trials\SoTrials\answers\13415663\test\df\2\abc
will delete E:\trials\SoTrials\answers\13415663\test\df\2\abc

证据:

tree /A ..\test
Folder PATH listing for volume eh
Volume serial number is 0ED6-233C
E:\TRIALS\SOTRIALS\ANSWERS\13415663\TEST
+---vbs
\---df
    +---1
    |   +---b
    |   |   \---x
    |   \---a
    \---2

您应该能够编写一个解决特定问题的脚本。

<强>更新

查看here以查看应用于移动文件夹的方法。

答案 1 :(得分:0)

好吧,你需要阅读所有文件夹,比较子文件夹,如果它符合你的名字,删除它。

使用它来获取子文件夹。

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("E:\test\43\5512686\5512698\html\")
Set fc = f.SubFolders

然后使用它删除您的文件夹。

Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists("E:\test\43\5512686\5512698\html\abc") Then 
   filesys.DeleteFolder "E:\test\43\5512686\5512698\html\abc"
End If

使用这些代码段,您可以根据需要轻松完成代码。

您似乎不确定父文件夹的不同名称,因此您必须从根文件夹开始并循环所有子文件夹并为每个文件夹级别执行此操作。