On unix it would seem that you are truncating the text files by opening them to write before you have read them, a result of opening them by the same name. I don't recall how that works on win32, but I suspect it is the same.

You can fix that by either using a temporary file name and renaming it after the copy is done, or else by opening in '+<' mode, which opens to read and write, without truncation.

Here's a rewrite using the non-truncating r/w open. I prefer it to the temporary file because of race conditions which may occur with temporary files. I'll use glob to simplify grabbing the file listing.

#!/usr/bin/perl use warnings; use strict; use Fcntl ':flock'; my $path = 'c:/perl/dvd_files/'; # only doing the problem files - those with unchanged names my @files = glob "$path*.txt"; for (@files) { local $/; open my $fh, '+<', $_ or warn($!), next; flock $fh, LOCK_EX; binmode $fh; my $text = <$fh>;
Your following substitution looks fishy. I can't tell what you really want to do, but what it does can be done by tr///,
$text =~ tr/0-9{}//d; seek $fh, 0, 0; truncate $fh, 0; print $fh $text; }
The code for the other files is similar, but there is not the issue of clobbering the file you are reading from. There is an issue of clobbering a text file if a .sub or .srt file has the same basename as a text file.

On reflection, I wonder if you wouldn't be better off creating the rewritten files in a brand-new subdirectory and not clobbering anything. That may be necessary, anyway, since I can't test on win32 and am not sure that flock, seek, or truncate will work on your system.

After Compline,
Zaxo


In reply to Re: reading text from afile by Zaxo
in thread reading text from afile by fadingjava

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.