avi1991nash has asked for the wisdom of the Perl Monks concerning the following question:

Here I am converting all ".txt" file into".dat" from respective folder. But I am geting output ".txt.dat" instead of ".dat".

#!/usr/bin/perl $runDir = $ENV{"REGR_RUN_DIR"}; @files = (<*.txt>); foreach $FILE (@files) { open (IN, $FILE) || die "Couldn't open $FILE for reading"; open (OUT, ">$runDir/$FILE") || die "Couldn't open $FILE for reading"; while(<IN>) { print OUT $_; } } close(IN); close(OUT);

Replies are listed 'Best First'.
Re: conveting .txt file to .dat
by Corion (Patriarch) on Oct 24, 2016 at 17:15 UTC

    The code you've shown cannot be the code responsible for that, because the code you've shown does overwrite the input file and does not mention .dat at all.

Re: conveting .txt file to .dat
by ww (Archbishop) on Oct 24, 2016 at 17:50 UTC

    We can't help you much when you post irrelevant code... but Corion's already mentioned that.

    But since your narrative remark sounds as though you're merely RENAMING files (ie, merely changing the Win-style extension or "file type" from .txt to .dat), you don't need to read the files and re-write them; just rename.


    Yet another gimmé request! Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.

    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. relevant (real) code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.

      Please see rename.


      Give a man a fish:  <%-{-{-{-<

Re: conveting .txt file to .dat
by GotToBTru (Prior) on Oct 24, 2016 at 20:34 UTC

    If you run this script from $runDir, you will wipe out your files without renaming them. If run from any other directory, it does achieve copying of the files to $runDir, but does so the hard way. Since you never give them new names, only a new directory, the names will stay the same. So, I conclude as my brethren have that this is not the script you need help with.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)