Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

VBS script for deleting a specific file from a folder and all the subfolders

Hi,
I need a script that will be pointed to a folder that has a lot of subfolders, and then go though all the subfolders and delete a specific file test.ini

Main folder is:  Profile
File need to be deleted:   test.ini

Thanks
0
rov17
Asked:
rov17
2 Solutions
 
Peter KwanCommented:
Please see if the attached code works for you.
Dim RootDir, FileName
Dim fs, rd, f, sf

FileName = "test.ini"

RootDirStr = "Your directory"
Set fs = CreateObject("Scripting.FileSystemObject")
Set rd = fs.GetFolder(RootDir)

DeleteFile rd

Sub DeleteFile(rd)
	If fs.FileExists(rd.Path & "\" & FileName) Then 
		fs.DeleteFile(rd.Path & "\" & FileName)
	End If

	For Each f in rd.SubFolders
		DeleteFile f		
	Next
End Sub

Open in new window

0
 
rov17Author Commented:
Thanks very much pkwan,
I tried to run the script got the error below:

Line 8, Char: 1
Error Invalid procedure call or argument. Code 800a0005
0
 
Peter KwanCommented:
Sorry for typo. Here is the correct version:

Dim RootDir, FileName
Dim fs, rd, f, sf

FileName = "test.ini"

RootDir = "Your directory"
Set fs = CreateObject("Scripting.FileSystemObject")
Set rd = fs.GetFolder(RootDir)

DeleteFile rd

Sub DeleteFile(rd)
	If fs.FileExists(rd.Path & "\" & FileName) Then 
		fs.DeleteFile(rd.Path & "\" & FileName)
	End If

	For Each f in rd.SubFolders
		DeleteFile f		
	Next
End Sub

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rov17Author Commented:
Thanks pkwan,
Tried it got the below error

Line 17, Char: 2
Error premission denied, code :800A0046

I am logged in as Admin.
0
 
Peter KwanCommented:
1) Are you RootDir pointing to a local folder on your PC?
2) Are you able to browse the subfolders in Windows Explorer?
0
 
rov17Author Commented:
RootDir pointing to "C:\Documents and settings"
Yes i am able to browse the subfolders in Windows Explorer
0
 
Peter KwanCommented:
Please see if the following solves your problem.

http://www.minasi.com/forum/topic.asp?TOPIC_ID=17296
0
 
Bill PrewCommented:
Why not just use the dos command like DEL command, it can do this in one shot:

DEL /S /Q profile\test.ini

~bp
0
 
prashanthdCommented:
This should do the needful, ensure you give full path of root directory
On Error Resume Next

root_dir="c:\profile" ' give full path of root directory

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(root_dir)
getInfo(objDir)

Sub getInfo(pCurrentDir) 
    
    For Each aItem In pCurrentDir.Files
        'WScript.Echo aItem.path
        If LCase(aItem.Name) = "test.ini" Then
            WScript.Echo aitem.name
            aitem.delete
        End If
    Next
    
    For Each aItem In pCurrentDir.SubFolders
        getInfo(aItem) 
    Next
    
End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now