What conversion do I need to perform on a file name that contains non-ASCII characters so that I can pass it to MP3::Info::get_mp3info? The code below prints "<file name>: File is empty" for files containing non-ASCII characters (e.g. "Blue Öyster Cult- (Don't Fear) The Reaper".) I want to know how to use the file names produced by console programs, so please no suggestions to rewrite the script so that it uses a completely different method to get the file names. Thanks.
Update 4:08PM: I found that converting the file name to a short name with Win32::GetShortPathName and passing the short file name to get_mp3info appears to work correctly, EVEN IF THE SHORT NAME ALSO HAS NON-ASCII CHARACTERS IN IT! (CDBurnerXP generates short names with those characteristics.) There seems to be something about long file names containing non-ASCII characters that makes get_mp3info unable to open the file.
use strict;
use warnings;
use Encode;
use MP3::Info;
my $dir = '';
binmode STDOUT, ":encoding(UTF-8)";
for my $rawline (`dir /s $ARGV[0]`)
{
chop($rawline); # remove newline
my $line = decode('cp437', $rawline);
if ($line =~ /^\d/ && substr($line, 24, 1) eq ' ')
{
# listing for a file, not a directory
my $extix = rindex($line, '.');
my $filename = substr($line, 39);
my $filepath = "$dir\\$filename";
my $mp3time = '';
if (lc(substr($line, $extix, 3)) eq '.mp')
{
# mpeg file
my $info = get_mp3info($filepath);
if (defined($info))
{
print "$filepath: time is $info->{TIME}\n";
}
else
{
print("$filepath: $@\n");
}
}
}
elsif (substr($line, 0, 14) eq ' Directory of ')
{
$dir = substr($line, 14);
$dir =~ s/\\$//;
print("$line\n");
}
}