Script to delete All files / folders in a specific directory.

Hello,

I am new to VB and want to create a simple script to delete all files and folders under a specific directory.

(i.e. c:\Program Files\test\test1)

In other words, I would like to delete everything in the test1 driectory.

If you have any ideas, please let me know.  


Thanks

Chad
16582Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nffvrxqgrcfqvvcCommented:
Refer here: This question was asked yesterday.
http:Q_21482816.html
16582Author Commented:
I read that and for deleting multiple directories, that's exactly what I need.  The problem lies int the statement above "I am new to VB ".  All I want is to be able to delete all the files in test1 including 1 subdirectory.

The reason being is a program I'm running ocassionally creates temp files and 1 directory that I need to get rid of.

I'm just  trying to test it out using VB.
nffvrxqgrcfqvvcCommented:
You can use this then..
'Remove all files in the C:\test directory
Call kill("C:\test\*.*")

If a file is present in the other subdirectory you must delete it before you can remove the directory itself
Call kill("C:\test\tes1\*.*")
Call RMDIR("C:\test\test1\")
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

nffvrxqgrcfqvvcCommented:
So it would look somehting like this...

Call Kill("C:\test\test1\*.*")
Call Kill("C:\test\*.*")
Call RmDir("C:\test\test1\")
16582Author Commented:
I must be doing something wrong.  

I'm putting in notepad

Call Kill("C:\test\test1\*.*")
Call Kill("C:\test\*.*")
Call RmDir("C:\test\test1\")

but when I save it as rm.vbs and run it I get the error

Script:     C:\test\test1
Line:       1
Char:      1
Error:     Type mismatch: 'Kill'
Code:     800A00D
Source:  Microsoft VCScript runtime error

Sorry, this is pretty basic but, I'm trying to figure it out.


nffvrxqgrcfqvvcCommented:
wow...I thought you said you were using VB...Your trying to use VBscript.
nffvrxqgrcfqvvcCommented:
Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="I:\Test\"

For Each file In fso.GetFolder(CleanPath).Files
file.delete
Next
16582Author Commented:
Sorry,

I didn't know that running the VB program would be that much different than creating a simple VBScript for MS Windows...  

16582Author Commented:
Now getting the following:

Script:     C:\rm.vbs
Line         5
Char:       1
Error:      Unexpected 'Next'
Code:      800A041F
Source:   Microsoft VBScript compilation error


I'm trying to read up on this but, making it work... different story.
nffvrxqgrcfqvvcCommented:
Sorry about that...to make it simple for you just add this to notepad save it as .VBS file

Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFolder "D:\test\test1", True
FSO.DeleteFile "D:\test\*", True
Set FSO= NOTHING

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
16582Author Commented:
Thanks!

That works exactly the way I need.  Are there any VBScripting books you could recommend?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.