Nishi Bhonsle wrote:
>
> Thanks for all your suggestions and help. I was able to do the following --
> 1)Flatten out the directory structure into a file containing the dir and
> files within the dir into
> C:\buildlist.txt
> 2)Read the lines from a file say files2.txt and modify the lines to contain
> the filenames fetched into C:\buildlist.txt. Write out the modified lines
> into result.txt.
>
> $path = "$ARGV[0]";
> opendir ( DIR, $path ) or die "Can't open $path: $!";
> while(defined($DIR = readdir DIR))
>    {
>     push(@array_A,$DIR)
>     }
> closedir DIR;
> open(FILE,">c:/buildlist.txt");
> foreach $y (@array_A)
> {
> next if $y =~ /^\./;
> push(@new,$y);
> print FILE "$y\n";
> }
> open (FILE1, "<c:/files2.txt") || die ("Could not open file. $!");
> @text = <FILE1>;
> open(FILE2,">>c:/results.txt");
> foreach $a(@text)
> {
> $y = shift(@new);
> $a =~ s/("[^"]*)("\s+"[^"]*)("\s+"[^"]*)("\s+NA)/$1$y$2$y$3$y$4/;
> print "$a\n";
> print FILE2 "$a";
> }close FILE2;
> close FILE1;
> close FILE;
>
> My sample files2.txt is
>
> "SL/" "%HOME%/server/bin/" "" NA
> "SL/" "%HOME%/server/bin/" "" NA
>
> My sample results.txt is
>
> "SL/one" "%HOME%/server/bin/one" "one" NA
> "SL/onetwo.txt" "%HOME%/server/bin/onetwo.txt" "onetwo.txt" NA
>
> How can I modify this program, so that instead of reading the lines from
> files2.txt, modifying them and writing them to results.txt, I can directly
> write out entire lines as in "SL/one" "%HOME%/server/bin/one" "one" NA to
> the results.txt(the only dynamic part of these entries, which will be
> different in each line will be the filename ie one, onetwo etc). So,
> depending on how many filenames are fetched into buidlist.txt, that many
> number of lines will be get added into results.txt

Hi Nishi

I think the program below is what you need. I've added 'use strict' and 'use
warnings' as these are pretty much indespensible in writing correct code.

I've economised by removing @array_A and putting the directory listing straight
into @new. I've also changed a regex that excludes filenames that consisted
entirely of dots; yours ignored only those that started with a dot.

I've thrown out files2.txt as I think you wanted, and also the @text array that
held its contents. The final loop is now just over @new, and each filename is
inserted at three points in a hard-coded string which is then both printed out to STDOUT and appended to results.txt as your code did.

I'm still not clear whether you need the buildlist.txt file, but I've left it
in as it was.

Please let us know if I've misunderstood anything.

HTH,

Rob


  use strict;
  use warnings;

  my $path = $ARGV[0];

  opendir DIR, $path or die "Can't open $path: $!";
  my @new = grep /[^.]/, readdir DIR;
  closedir DIR;

  open FILE,">c:/buildlist.txt";
  print FILE "$_\n" foreach @new;
  close FILE;

  open(FILE2,">>c:/results.txt");

  foreach my $file (@new) {
    my $record = qq("SL/$file" "%HOME%/server/bin/$file" "$file" NA\n);
    print $record;
    print FILE2 $record;
  }

  close FILE2;


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to