in reply to encode files to utf-16 and then move

heh, I bet I wrote that code. One thing I learned since then is that :raw:encoding(...) disables buffering. You want :raw:perlio:encoding(...).

You probably want to specify UTF-16le instead of UTF-16 since you probably want UTF-16le and not UTF-16be.

The snippet is already producing a copy, so all you need to do to make it move is to delete the source once the copy is created.

#!/usr/bin/perl use strict; use warnings; my ($src_qfn, $dst_qfn) = @ARGV; open(my $src_fh, "<:raw:perlio:encoding(UTF-8)", $src_qfn) or die("Can't open \"$src_qfn\": $!\n"); open(my $dst_fh, ">:raw:perlio:encoding(UTF-16le)", $dst_qfn) or die("Can't open \"$src_qfn\": $!\n"); print $dst_fh $_ while <$src_fh>; unlink($src_fh);

Replies are listed 'Best First'.
Re^2: encode files to utf-16 and then move
by tannx (Acolyte) on May 11, 2009 at 13:08 UTC
    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; }
      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";
      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;
Re^2: encode files to utf-16 and then move
by tannx (Acolyte) on May 25, 2009 at 13:04 UTC
    You right I need utf-16le but if i define output utf-16le then the file becomes unusable - 1NUL;NUL;2NUL;testNUL; etc. If defined UTF-16 then output is - 1;;2;test; but big endian. ----- It's probably because may source files are utf8 wo BOM and destinatin files are also without BOM.