freelanceprogrammers.org Forum Index » Perl
reading files recursively
Joined: 18 Jul 2004
Posts: 1
reading files recursively
Hello Friends,
I am facing one problem on windows perl. I have to rename 100
index.html pages to index.asp recursively. Before renaming the page
i need to add one line of code before </body> tag in each file.
There are 100 directories exist for this task. Anyone help me how
to do this task.
thanks
pskantu
Joined: 19 Jul 2004
Posts: 4
reading files recursively
Pskantu,
I am assuming all the files/directories are in the same directory tree
and that you have a fairly good understanding of Perl. If I`m correct
this might give you an idea.
Go to a command prompt and get to the highest level folder with the
Index file in it. Then do a "dir /ad /s /b >dirs.txt". This will
give you a text file called dirs.txt that is a list of all sub folders
under the current folder. The /ad give you folders only, the /s gets
the sub folders, and /b is bare format so you only get the name. Now
write a perl script to open dirs.txt and then loop through until the
end of the file. Each pass through the loop should read a line from
dirs.txt to get a folder name. Then check for the index.html in the
folder. If it`s there, add your text and rename the file then loop
and goto the next folder in dirs.txt.
Hope this gives you some ideas.
Chris
--- In perl_official@yahoogroups.com, "pskantu" <pskantu@y...> wrote:
> Hello Friends,
>
> I am facing one problem on windows perl. I have to rename 100
> index.html pages to index.asp recursively. Before renaming the page
> i need to add one line of code before </body> tag in each file.
>
>
> There are 100 directories exist for this task. Anyone help me how
> to do this task.
>
> thanks
> pskantu
Joined: 16 Jun 2004
Posts: 9
reading files recursively
> Pskantu,
>
> I am assuming all the files/directories are in the same directory tree
> and that you have a fairly good understanding of Perl. If I`m correct
> this might give you an idea.
>
> Go to a command prompt and get to the highest level folder with the
> Index file in it. Then do a "dir /ad /s /b >dirs.txt". This will
> give you a text file called dirs.txt that is a list of all sub folders
> under the current folder. The /ad give you folders only, the /s gets
> the sub folders, and /b is bare format so you only get the name. Now
> write a perl script to open dirs.txt and then loop through until the
> end of the file. Each pass through the loop should read a line from
> dirs.txt to get a folder name.
May be easier:
$folder_to_search="c:\top_dir_with_all_my_folders_in_it";
opendir(dir,$folder_to_search);
@dirs_list=grep {
!(/^./) && -f "$folder_to_search/$_"
}
readdir(dir);
closedir (dir);
rewinddir(dir);
Now you have a list of folders, you can do your file check: open the file,
find "</body>" and replace with "$additional_string/n</body>" and save the
file as the new name. Then delete the old filename.
>Then check for the index.html in the
> folder. If it`s there, add your text and rename the file then loop
> and goto the next folder in dirs.txt.
>
> Hope this gives you some ideas.
>
> Chris
>
> --- In perl_official@yahoogroups.com, "pskantu" <pskantu@y...> wrote:
>> Hello Friends,
>>
>> I am facing one problem on windows perl. I have to rename 100
>> index.html pages to index.asp recursively. Before renaming the page
>> i need to add one line of code before </body> tag in each file.
>>
>>
>> There are 100 directories exist for this task. Anyone help me how
>> to do this task.
>>
>> thanks
>> pskantu
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
Joined: 19 Jul 2004
Posts: 1
reading files recursively
Currently I would tend to use something along this line:
#!perl
@files = `dir /s /b index.html`;
chomp @files;
foreach $file (@files) {
print "Reading $file
";
open(FH, "< $file");
while (<FH>) {
if ($_ =~ /</body>/i) {
push @file_content, "new line
";
push @file_content, $_;
} else {
push @file_content, $_;
}
}
close FH;
$file =~ s#.html#.asp#i;
open(FH2, "> $file");
print "Writing $file
";
print FH2 @file_content;
close FH2;
}
__END__
After insuring it did what I wanted done, I might replace everthing
inside the foreach loop with `del $file`; to remove the index.html
files.
Have fun,
Gary
> --- In perl_official@yahoogroups.com, "pskantu" <pskantu@y...> wrote:
> > Hello Friends,
> >
> > I am facing one problem on windows perl. I have to rename 100
> > index.html pages to index.asp recursively. Before renaming
> the page
> > i need to add one line of code before </body> tag in each file.
> >
> >
> > There are 100 directories exist for this task. Anyone help me how
> > to do this task.
> >
> > thanks
> > pskantu
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~-->
> Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
> Now with Pop-Up Blocker. Get it for free!
> http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/ndFolB/TM
> --------------------------------------------------------------
> ------~->
>
>
> Yahoo! Groups Links
>
>
>
>
>
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







