0

我需要 VBScript / 命令提示符来删除具有特定名称的子文件夹

例如:

  1. E:\test\43\5512686\5512698\html\abc
  2. E:\test\43\5467686\5512699\html\abc
  3. E:\test\43\5587686\55147589\html\abc
  4. E:\test\45\5517586\5512698\html\abc

ETC。,

其中上述示例“abc”文件夹需要删除

有人可以帮忙吗

提前致谢

4

2 回答 2

1

对于背景/上下文,请参见递归文件访问框架。鉴于此文件夹结构:

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

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

更新:

在这里查看应用于移动文件夹的方法。

于 2012-11-17T09:44:34.027 回答
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

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

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

于 2012-11-17T09:09:15.527 回答