VB Script to Delete specific subfolder

耗尽温柔 提交于 2020-01-11 13:43:51

问题


I am in need of VBScript / command-prompt to delete subfolder which will have specific name

For Eg.:

  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.,

wherein above example "abc" folder needs to delete

Can anybody help on this

Thanks in advance


回答1:


For background/context see this skeleton for recursive file access. Given this folder structure:

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

and this proof of concept code:

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

Output:

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

Evidence:

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

you should be able to write a script that solves your specific problem.

UPDATE:

Look here to see the approach applied to moving folders.




回答2:


Well, you need to read all folders, compare the subfolder and if it fits your name, delete it.

Use this to get your subfolders.

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

Then use this to delete your folder.

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

With those snippets you can complete easy the code on your demands.

It seems you don't know for sure the different names of the parent folders, so you have to start at the root folder and loop all subfolders and do this for each folder level.



来源:https://stackoverflow.com/questions/13428869/vb-script-to-delete-specific-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!