Re: write out filenames of files existing on a filesystem into afile



Hi:

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

Please let me know. Thanks.



On 7/8/06, Rob Dixon <rob.dixon@xxxxxxx> wrote:

(Randal L. Schwartz) wrote:

Hi Randal

>>>>>>"Rob" == Rob Dixon <rob.dixon@xxxxxxx> writes:
>
>
> Rob> I favour
>
> Rob> print FILE "$_\n" foreach grep /[^.]/, readdir DIR;
>
> Rob> for clarity
>
> But certainly not for correctness! It's easy to look pretty if you
don't care
> about the right answer. Your regex rejects *any* filename that contains
*any*
> dot. On a windows system, that'd be just about any file. :)
>
> Nahh, best to ignore yours and look upthread heere.

Look again. My regex rejects any filename that contains /only/ dots.
/[^.]/
matches any character that isn't a dot.

>
>>>closedir DIR;
>>>close FILE;
>
>
> Rob> Or even
>
> Rob> system "dir /b $path > C:/filelist.txt";
> Rob> $? and die "Can't create C:/filelist.txt: $?"
>
> Rob> since portability is not a question
>
> Apparently, since your code isn't portable either. :)

I don't understand this comment. The only lack of portability I can see is
in
the form of the file paths, which was in the original post.

>>>s/("[^"]*)("\s+"[^"]*)("\s+"[^"]*)("\s+NA)/$1$file$2$file$3$file$4/;
>
>
> Rob> Again, I think
>
> Rob> s/("[^"]*)(")/$1$file$2/g;
>
> Rob> is less befuddling.
>
> And less correct.

I don't see why? This was the request:

>>> Can you also let me know how to read the line in the below file, look
>>> for
>>> the second and forth " and write the name of the file fetched in the
>>> above program before the second and fourth " and between the fifth and
>>> sixth
>>> " one by one.
>>>
>>> "SL/" "%HOME%/server/bin/" "" NA
>>> as in
>>> "SL/one" "%HOME%/server/bin/one" "one" NA

John's approach was to match the strings

'"SL/'
'" "%HOME%/server/bin/'
'" "'
and
'" NA'

and to put them back into the replacement string with the filename
interposed. I
saw it more simply as just putting the filename before the closing quote
of all
embedded quoted strings. Either way, my code works with the given data, so
unless you've seen a case where it doesn't hold up I can't see the
problem.

> Hmm. Your track record isn't good on this post.

Please explain youself Randal. Or did you just wake up in troll mode
today?

> Perhaps you should read and learn more before answering, because a bad
answer
> is a lot more damaging than *no* answer, especially when correct answers
> have already been given elsewhere in the thread.

I do read and I do learn, and learning is one of my main reasons for
subscribing
to this mailing list. A bad answer is certainly damaging, but I still
don't
think my post was wrong. Perhaps more destructive still is an abusive
rejoinder.

> We appreciate your enthusiasm to help. But in this case, "help" means
> "please spend more time reading and less time posting". Or at least
> check your answers before posting... that would have been caught here.

I checked my answers before posting them. Did you check them before
rubbishing them?

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>





Relevant Pages

  • Re: How do I get pictures in a report to change with each record?
    ... I copied the code from an application of mine, where I've got the pathname ... so I'm only storing the filename in the table. ... >> Hi Rob, ... >> HTH ...
    (microsoft.public.access.reports)
  • Re: Filename problem
    ... Rob and Frank, ... changed the subroutine call to use a hard-wired "Joe" instead of this ... starting string, and it found the file just fine with that. ... I used that same string in another prior filename ands it ...
    (microsoft.public.excel.programming)
  • Re: Pass between modules
    ... I have declared Public FileName As String in module 1 but regardless what I do in module 2, I can't capture FileName declare in module 1. ... I have Option Explicit set in all modules and subs aren't set to Private. ... Thanks, Rob ...
    (microsoft.public.excel.programming)
  • Re: Arrghh.. Sockets are driving me nuts!!! :-(
    ... "Ehtor" wrote in message ... Thanks Rob, ... but I want to have the exact filename. ...
    (alt.php)
  • Re: Transferring directory structure to Windows
    ... Transferring directory structure to Windows ... Filename collisions are a real threat but it should be ... whether FTP, tar, or whatever, you will only wind up ...
    (AIX-L)

Loading