Solved

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

Posted on 2011-02-13
9
371 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 53

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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