in reply to Re: encode files to utf-16 and then move
in thread encode files to utf-16 and then move

Right now it processes only one file.
#!/usr/bin/perl use warnings; use strict; my $srcdir = "C:\\ROOT_DIR\\test1\\"; my $dest = "C:\\ROOT_DIR\\test2\\"; my (@files); for (;;) { opendir(DIR, $srcdir) or die "Can't open $srcdir: $!"; @files = grep {!/^\.+$/} readdir(DIR); close(DIR); if (!@files) { print "done.\n\n"; last; } my $file = $files[0]; open(my $src_fh, "<:raw:perlio:encoding(UTF-8)", "$srcdir$file") or die("Can't open \"$srcdir$file\": $!\n"); open(my $dst_fh, ">:raw:perlio:encoding(UTF-16)", "$dest$file") or die("Can't open \"$dest$file\": $!\n"); print $dst_fh $_ while <$src_fh>; unlink($src_fh); sleep 1; }

Replies are listed 'Best First'.
Re^3: encode files to utf-16 and then move
by ikegami (Patriarch) on May 11, 2009 at 14:11 UTC
    Moved reading the directory to the outside of the loop and some other cleanup:
    #!/usr/bin/perl use warnings; use strict; use File::Spec::Functions qw( catfile ); my $src_dir = "C:\\ROOT_DIR\\test1\\"; my $dst_dir = "C:\\ROOT_DIR\\test2\\"; opendir(my $dh, $srcdir) or die "Can't open dir $srcdir: $!\n"; while (defined(my $file = readdir($dh))) { next if /^\.\.?\z/; my $src_file = catfile($src_dir, $file); my $dst_file = catfile($dst_dir, $file); open(my $src_fh, "<:raw:perlio:encoding(UTF-8)", $src_file) or die("Can't open \"$src_file\": $!\n"); open(my $dst_fh, ">:raw:perlio:encoding(UTF-16)", $dst_file) or die("Can't open \"$dst_file\": $!\n"); print $dst_fh $_ while <$src_fh>; unlink($src_fh); } print "done.\n";
Re^3: encode files to utf-16 and then move
by Anonymous Monk on May 11, 2009 at 13:19 UTC
    close $dst_fh or die qq!Failed close "$dest$file" :$!: $^E!; close $src_fh or die qq!Failed close "$srcdir$file": $!: $^E!; unlink "$srcdir$file" or die qq!Failed unlink "$srcdir$file": $!: $^E! +; sleep 1;