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.
Your following substitution looks fishy. I can't tell what you really want to do, but what it does can be done by tr///,#!/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>;
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.$text =~ tr/0-9{}//d; seek $fh, 0, 0; truncate $fh, 0; print $fh $text; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |