Solved

How to delete all the text file in a folder

Posted on 2009-05-07
5
591 Views
Last Modified: 2012-06-27
How to delete all the text file in a folder
0
Comment
Question by:tankergoblin
5 Comments
 
LVL 3

Expert Comment

by:mharfouch
ID: 24332909
#!/usr/bin/perl

print "content-type: text/html \n\n";      #The header
@files = ("newtext.txt","moretext.txt","yetmoretext.txt");
foreach $file (@files) {
    unlink($file);
}
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24332915
Hi tankergoblin,

try

unlink <*.txt> or die "Cannot unlink files $!";

or, more safely

if () {
    my $c = unlink  or die "Cannot do it $!";
    print "deleted ", $c, " files","\n";
} else {
    print "nothing to delete","\n";
}

lwadwell
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24332943

#This will work, but will only die if it doesn't remove any

#If any files are removed (even if some could not be), you will not be notified

unlink </path/to/*.txt> or die "Cannot unlink files $!";
 
 

#### With this, you will be notified for each unsuccessful delete

foreach (</path/to/*.txt>) {

    unlink or warn "Could not remove $_:$!\n";

}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24333279
#depending on how you define  text file
-T and unlink || warn "$_ $!" for <folder/*>;
0
 
LVL 5

Accepted Solution

by:
vikaskhoria earned 500 total points
ID: 24333687
Check the script below, it will remove any .txt files from the given directory.
Do let me know, if you have any doubts.

Vikas.

#!/usr/bin/perl
 

$dirname = <STDIN>;

chomp($dirname);

opendir(DIR,$dirname);

while ( $file = readdir(DIR))

 {

  if ( $file =~ /\.txt$/)

  { 

    unlink $dirname."/".$file;

  }

} 

closedir(DIR);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries PERL Scripts 7 147
.properties file to call function/method 9 59
how to exit a  for loop inside a function with return value in bash 5 96
Perl tar error 8 50
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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 …
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

12 Experts available now in Live!

Get 1:1 Help Now