Solved

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

Posted on 2011-02-13
9
347 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell: Try/Catch help 2 30
Powershell to track mobile activity 2 44
Password Expiry 9 21
Sending Attachment via CDO 3 22
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now