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
LVL 5
rov17Asked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
 
Peter KwanAnalyst ProgrammerCommented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Peter KwanAnalyst ProgrammerCommented:
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
 
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 KwanAnalyst ProgrammerCommented:
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 KwanAnalyst ProgrammerCommented:
Please see if the following solves your problem.

http://www.minasi.com/forum/topic.asp?TOPIC_ID=17296
0
 
prashanthdConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.