Reputation: 11
I am in need of VBScript / command-prompt to delete subfolder which will have specific name
For Eg.:
etc.,
wherein above example "abc" folder needs to delete
Can anybody help on this
Thanks in advance
Upvotes: 0
Views: 3297
Reputation: 38745
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.
Upvotes: 1
Reputation: 6222
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.
Upvotes: 0