Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

open file and write the content into another file

by cesapun (Acolyte)
on Sep 09, 2011 at 10:45 UTC ( #925054=perlquestion: print w/ replies, xml ) Need Help??
cesapun has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; my $folder = $ARGV[0]; my $output = 'C:\Users\AndreyT\Desktop\tee.txt'; foreach(glob "$folder\*.nfo"){ use_contents($_); } sub use_contents{ my $filename = shift; open my $fh, '<$filename' or die "Can't open $filename: $!"; open my $dat1, '>$output' or die "\nERROR: Cannot create/open $out +put\n"; while(<$fh>){ print $dat1 $_;} close ($fh); close ($dat1); }

This code doesn't working

I'm trying to open a file from a folder and write the content into another file

What i'm doing wrong?

Thanks in advance

Comment on open file and write the content into another file
Download Code
Re: open file and write the content into another file
by ambrus (Abbot) on Sep 09, 2011 at 10:58 UTC

    Replace glob "$folder\*.nfo" with glob "$folder\\*.nfo" so the backslash doesn't disappear.

    Replace open my $fh, '<$filename' with open my $fh, '<', $filename because the single quotes in your code don't allow interpolation of the variable. Change the second open similarly. (Thanks to bart for noticing this second error.)

    Also, you're overwriting the same output file each time, so in the end you'll only get a copy of one file, the one glob finds last.

    If you want to debug such code alone, it could be worth to add a statement like warn "copying ($filename)\n"; to the use_contents function, so that you can see how many times the function gets called and for what files.

Re: open file and write the content into another file
by cesapun (Acolyte) on Sep 09, 2011 at 11:03 UTC
    now it's working....many,many thanks
Re: open file and write the content into another file
by perl_lover (Chaplain) on Sep 09, 2011 at 11:07 UTC
    Variable interpolation happens only inside double quotes.
    Change this
    open my $fh, '<$filename' or die "Can't open $filename: $!"; open my $dat1, '>$output' or die "\nERROR: Cannot create/open $out
    to
    open my $fh, "<$filename" or die "Can't open $filename: $!"; open my $dat1, ">$output" or die "\nERROR: Cannot create/open $out

    use Perl;
    Perl4Everything
      Variable interpolation happens only inside double quotes. Change this ... to ...

      And perhaps even better, change to the three-argument form of open:
          open my $fh, '<', $filename or die "Can't open '$filename': $!";
      (no need to string-interpolate  $filename in  open call, quoting  $filename in error message makes all whitespace explicit).

Re: open file and write the content into another file
by rovf (Priest) on Sep 09, 2011 at 11:34 UTC
    In addition to what ambrus said, I suggest that you use only forward slashes in the string passed to glob. From File::Glob:

    Win32 users should use the real slash. If you really want to use backslashes, consider using Sarathy's File::DosGlob, which comes with the standard Perl distribution.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: open file and write the content into another file
by RichardK (Priest) on Sep 09, 2011 at 11:41 UTC

    If you just want to copy files you could use File::Copy

    use File::Copy; copy($src,$dst);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://925054]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-07-26 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls