Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting of files in a dir

Posted on 1999-06-30
8
Medium Priority
?
196 Views
Last Modified: 2010-04-29
Hello,

I have the following question:

I have a dir with a few files in it.
The names of the files are not known.
The names are made by a script and look like:
376b3a456167cef5.cart
376b831d4c47d56e.cart
376d20d7664837a9.cart
etc.

Now I want to delete the files.
I want to have tow options:
First: deleting all files is this dir.
Second: Deleting a specified file or files

Is this possible?
And how is it done!

greeitings Michel Weegerink
0
Comment
Question by:mmcw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213672
1. Delete all files

@files = <*.cart>;
foreach $file (@files)
{
   unlink($file);
};

2. Delete specified files. Just chanage the file spec to something like 376b3*.cart

If you want to get fancy, you can also use the opendir and readir functions to achieve the same thing.



0
 

Author Comment

by:mmcw
ID: 1213673
Hello,

Thank you for your quick answer.

But what do you mean by the commands

opendir and readir

I do not know a lot of perl.

Is it possible to explain it for me with an example?

0
 
LVL 84

Expert Comment

by:ozo
ID: 1213674
unlink <*.cart>;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213675
Just to explain the opendir approach:

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = grep { /\.cart$/ } readdir(DIR);
foreach $file (@files) { unlink($file); }
closedir DIR;

0
 
LVL 84

Expert Comment

by:ozo
ID: 1213676
# perldoc -f readdir
$some_dir = '.'
opendir DIR, $some_dir  or die "can't opendir $some_dir because $!";
while( defined($_ = readdir DIR) ){
   next unless /\.cart$/;
   unlink "$some_dir/$_" or die "can't unlink $some_dir/$_ because $!";
}
closedir DIR;
0
 

Author Comment

by:mmcw
ID: 1213677
I don't know why but I did not mean to reject the answer.
I was excelent instead. Pleaes tell me how I can give the points to teraplane
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213678
Just let teraplane answer again.
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 400 total points
ID: 1213679
Thanks mmcw and ozo. I noticed that ozo improved on my answer, and I had an error in the second part, so here is a combined corrected version

unlink <*.cart>; #delete all files in current directory
unlink <376b3*.cart> #delete some files

# alternatively, use the pendir-readdir approach
opendir(DIR, $some_dir) or die "can't opendir $some_dir: $!";
@files = grep { /\.cart$/ } readdir(DIR);
foreach $file (@files)
{
   unlink("$some_dir/$file") or
      die "can't unlink $some_dir/$file because $!";

}
closedir DIR;




0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

670 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