Solved

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

Posted on 2011-02-13
9
383 Views
Last Modified: 2012-05-11
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
Comment
Question by:rov17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 34881898
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
 
LVL 5

Author Comment

by:rov17
ID: 34882164
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
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 34882198
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 5

Author Comment

by:rov17
ID: 34882256
Thanks pkwan,
Tried it got the below error

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

I am logged in as Admin.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 34882373
1) Are you RootDir pointing to a local folder on your PC?
2) Are you able to browse the subfolders in Windows Explorer?
0
 
LVL 5

Author Comment

by:rov17
ID: 34882404
RootDir pointing to "C:\Documents and settings"
Yes i am able to browse the subfolders in Windows Explorer
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 34882453
Please see if the following solves your problem.

http://www.minasi.com/forum/topic.asp?TOPIC_ID=17296
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 350 total points
ID: 34882580
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
 
LVL 12

Assisted Solution

by:prashanthd
prashanthd earned 150 total points
ID: 34896166
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question