473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Deleting the matchin pattern form all the files

123 100+
Hi..
I have one directory which contains huge amount of .php files.
I want to delete
Expand|Select|Wrap|Line Numbers
  1. <p align="right"><input name="closeme" type="button" value="Close Window" onclick="self.close()" /></p> <br />
---- Close window code form each of the pages.
So i need some perl subroutine which can do that for all the files in the directory..
Apr 16 '08 #1
5 1441
eWish
971 Expert 512MB
You will need to so us what you have tired. We will assist those who try and get stuck.

--Kevin
Apr 16 '08 #2
ajd335
123 100+
Hi ..I tried out tht 1..Bt shows me errors..There may be some errors as i just started working on perl.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perL
  2. <!-- use File::Copy qw(copy);-->
  3.  
  4. use strict;
  5.  
  6. my $re_file = 'pattern.php';
  7. my $dir_name = "home/davea/test";
  8.  
  9. opendir(DIR, $dir_name) or die "Can't open $dir_name: $!";
  10. while (readdir(DIR)) {
  11.         my $file = $_;
  12.         print REPORTFH "$file\n";
  13. open(REPORTFH, "> $re_file") or die "Can't read $re_file: $!";
  14.  
  15. while (readdir(DIR)) {
  16.         my $file = $_;
  17.         print REPORTFH "$file\n";
  18.  
  19.         if (! -e "$dir_name/$file") {
  20.                 print "Can't find the file $file. Please verify in the $re_file\n";
  21.         } elsif ($file =~ /PC/) {
  22.                 my $source = "$dir_name/$file.php";
  23.                 my $target = "$dir_name/$file";
  24.  
  25.                 copy($target, $source) or die "not working";
  26.                 open(IN, $source) or die "Can't open $source: $!";
  27.                 open(OUT, "> $target") or die "Can't open $target: $!";
  28.  
  29.                 my $truncateMode = 0;
  30.  
  31.                 while (IN) {
  32.                         if (! $truncateMode) {
  33.                                 if (/ <p align="right"><input name="closeme" type="button" value="Close Window" onclick="
  34. self.close() /)
  35.                                  {
  36.                                         $truncateMode = 1;
  37.                                 } else {
  38.                                         print OUT $_;
  39.                                        }
  40.                                             }
  41.                            }
  42.                 }
  43.  
  44.                 close (IN);
  45.                 close (OUT);
Apr 16 '08 #3
KevinADC
4,059 Expert 2GB
Are you trying to embed that into an html file? You can't. This will also cause perl to drop dead:

<!-- use File::Copy qw(copy);-->

as it is a syntax error and will not allow the perl script to compile.
Apr 16 '08 #4
ajd335
123 100+
Are you trying to embed that into an html file? You can't. This will also cause perl to drop dead:

<!-- use File::Copy qw(copy);-->

as it is a syntax error and will not allow the perl script to compile.
No , I have created .pl file and not the HTML and that is using VI editor...and the target files are PHP pages from which i have to remove the desire code

and when i run the code it gives me below errors :

Bareword "IN" not allowed while "strict subs" in use at close1.pl line 42.
Global symbol "$source" requires explicit package name at close1.pl line 47.
Global symbol "$source" requires explicit package name at close1.pl line 47.
Unmatched right curly bracket at close1.pl line 49, at end of line
syntax error at close1.pl line 49, near "}"
Execution of close1.pl aborted due to compilation errors.
Apr 16 '08 #5
eWish
971 Expert 512MB
Maybe this will help. I have commented sections of the code to help understand what is taking place.

Expand|Select|Wrap|Line Numbers
  1. # Directory to read
  2. my $my_directory = '/path/to/dir';
  3.  
  4. # Pattern for the search and replace
  5. my $pattern = qr/pattern here/;
  6.  
  7. # Get the files in said directory
  8. opendir(my $DIR, $my_directory) || die "Can't open $my_directory: $!\n";
  9. my @list = readdir($DIR);
  10. closedir($DIR);
  11.  
  12. # Loop through the files
  13. foreach  my $file (@list) {
  14.  
  15.         # Exclude '.' and '..'
  16.         next if $file eq '.'  || 
  17.                 $file eq '..';
  18.  
  19.     # Split the filename into the name and extension                        
  20.     my ($name, $ext) = split(/\./, $file);
  21.  
  22.     # Open new and existing file    
  23.     open(my $NEW_FILE, '>', "$my_directory/$name" . '_temp.' . $ext) || die "Can't open file", $name . '_temp.' . $ext, ':', $!, "\n";
  24.     open(my $OLD_FILE, '<', "$my_directory/$file") || die "Can't open file $file: $!\n";
  25.     while (<$OLD_FILE>) {
  26.  
  27.         # Replace the data as desired
  28.         s/$pattern//mgx;
  29.  
  30.         # After replacement the print to the file
  31.         print $NEW_FILE $_;
  32.     }
  33.  
  34.     # Close the files
  35.     close($OLD_FILE);
  36.     close($NEW_FILE);
  37.  
  38.     # Delete the original file
  39.     unlink("$my_directory/$file") || die "Can't unlink/delete $file: $!";
  40.  
  41.     # Rename the new file to the same as the original file
  42.     rename("$my_directory/$name" . '_temp.' . $ext, "$my_directory/$file")  || die "Can't rename $file: $!";
  43.  
  44. }
--Kevin
Apr 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
1
by: belmontpress | last post by:
A bit of a newbie question but how do you delete all files in a directory that match a given filename pattern. e.g. delete all of the ..txt files in the C:\bill directory?
6
by: belmontpress | last post by:
I wish to delete some files from a directory after reading them but have the problem that the system says that the files are in use and cannot delete them even though I have set the StreamReader to...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
10
by: kriz4321 | last post by:
I have many files in a dirctory in which I need to make the common subsitution. I need to delete all lines between two matched patterns I need to match a line having words " chkstats to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.