例如,给定一个空文件テスト.txt
,我该如何制作一个名为テスト.txt.copy
?
我第一次破解它设法访问该文件并创建新文件名,但生成的副本テスト.txt.copy
.
这是我的第一次尝试:
#!/usr/bin/env perl
use strict;
use warnings;
use English '-no_match_vars';
use File::Basename;
use Getopt::Long;
use File::Copy;
use Win32;
my (
$output_relfilepath,
) = process_command_line();
open my $fh, '>', $output_relfilepath or die $!;
binmode $fh, ':utf8';
foreach my $short_basename ( glob( '*.txt') ) {
# skip the output basename if it's in the glob
if ( $short_basename eq $output_relfilepath ) {
next;
}
my $long_basename = Win32::GetLongPathName( $short_basename );
my $new_basename = $long_basename . '.copy';
print {$fh} sprintf(
"short_basename = (%s)\n" .
" long_basename = (%s)\n" .
" new_basename = (%s)\n",
$short_basename,
$long_basename,
$new_basename,
);
copy( $short_basename, $new_basename );
}
printf(
"\n%s done! (%d seconds elapsed)\n",
basename( $0 ),
time() - $BASETIME,
);
# === subroutines ===
sub process_command_line {
# default arguments
my %args
= (
output_relfilepath => 'output.txt',
);
GetOptions(
'help' => sub { print usage(); exit },
'output_relfilepath=s' => \$args{output_relfilepath},
);
return (
$args{output_relfilepath},
);
}
sub usage {
my $script_name = basename $0;
my $usage = <<END_USAGE;
======================================================================
Test script to copy files with a UTF-8 filenames to files with
different UTF-8 filenames. This example tries to make copies of all
.txt files with versions that end in .txt.copy.
usage: ${script_name} (<options>)
options:
-output_relfilepath <s> set the output relative file path to <s>.
this file contains the short, long, and
new basenames.
(default: 'output.txt')
----------------------------------------------------------------------
examples:
${script_name}
======================================================================
END_USAGE
return $usage;
}
以下是内容output.txt
执行后:
short_basename = (BD9A~1.TXT)
long_basename = (テスト.txt)
new_basename = (テスト.txt.copy)
我尝试过更换文件::复制 http://perldoc.perl.org/File/Copy.html的复制命令与系统调用:
my $cmd = "copy \"${short_basename}\" \"${new_basename}\"";
print `$cmd`;
并使用 Win32::CopyFile:
Win32::CopyFile( $short_basename, $new_basename, 'true' );
不幸的是,在这两种情况下我得到了相同的结果(テスト.txt.copy
)。对于系统调用,打印显示1 file(s) copied.
正如预期的那样。
Notes:
- 我正在运行 Perl 5.10.0草莓 Perl http://strawberryperl.com/在 Windows 7 专业版上
- 我用Win32 http://search.cpan.org/~jdb/Win32-0.39/Win32.pm访问长文件名的模块
- glob 返回短文件名,我必须使用它来访问文件
- テsuto = 测试 (tesuto) inkatakana http://en.wikipedia.org/wiki/Katakana
- 我读了佩鲁尼图特 http://perldoc.perl.org/perlunitut.html and 每个软件开发人员绝对必须了解 Unicode 和字符集的绝对最低限度(没有任何借口!) http://joelonsoftware.com/articles/Unicode.html