Delete all files in a directory

I'm using Perl 5 on a WINDOWS NT server. It doesnt seem to accept the deltree command and besides I would like a more methodical way of deleting files. I can use the del command but it is waiting for confirmation if I send it the "*" wildcard. But if I could get a list of the files in a directory and set them in an array, and then iterate through them one at a time, I think this would be a solid answer. But how do I get a diretory list into an array?
microfleetAsked:
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.

guadalupeCommented:
     #Open dir handle
      opendir(dir,$dir);


            @files=grep {!(/^\./) && !(-d "$dir/$_")} readdir(dir);
      

      #loop over files
      foreach $file (@files)
      {
            #del file - remember to refer to complete path by usuing $dir/$file
      }



      closedir (dir);
0

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
guadalupeCommented:
To delete the file you could use perls command togeather with the variables defined in the loop like this:

unlink("$dir/$file");
0
guadalupeCommented:
You could also use this but some how I would consider "delete *.*" a risky move:

if (chdir("D:/Scratch/temp2/temp"))
{
      unlink<*.*>;
}

Note that though you are in windows you should use unix style forward slashes to change dir.  Back slashes will merely cause the chdir to fail and nothing will be deleted.
0
microfleetAuthor Commented:
I actually figured out a way to get the files to delete. I discoved the opendir function in my book and then it was just a matter of using the WINDOWS del call. But I like your way better because it doesnt limit itself to WINDOWS systems. Thanks. I will be posting another question now that deals with removing the directory. I thought I had that settled but it seems that after I remove all the files, the directory gives me an access error.
0
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
Perl

From novice to tech pro — start learning today.