Why does the -e file existence test always return false for a filename containing non-ASCII characters?

N. Pamnani

I am doing an existence check for a file containing non-ASCII characters in its name, using Perl. Even though the file exists, the check always returns false. I am using Strawberry Perl v5.24.0 on a Windows 10 machine.

Here is my code:

use strict;
use warnings;

use Encode;

my $file = '<SOME DIR PATH> / áéíóú.mov';
if (-e $file) {
  print "$file exists";
} else {
  print " $file does not exists" ;
}

I also changed the code page in the cmd shell by running chcp 65001. cmd was then able to recognize the characters but somehow it always returns "Not Exists" for this file.

How can I fix this?

ikegami
use strict;
use warnings;

# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;

# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
   $enc_input   = 'cp'.Win32::GetConsoleCP();
   $enc_output  = 'cp'.Win32::GetConsoleOutputCP();
   $enc_syscall = 'cp'.Win32::GetACP();

   binmode STDIN,  ":encoding($enc_input)";
   binmode STDOUT, ":encoding($enc_output)";
   binmode STDERR, ":encoding($enc_output)";
}

use Encode qw( encode );

my $file = 'áéíóú.mov';

if (-e encode($enc_syscall, $file, Encode::FB_CROAK | Encode::LEAVE_SRC)) {
   print("$file exists\n");
}
elsif ($!{ENOENT}) {
   print("$file doesn't exist\n");
}
else {
   die("Can't determine if \"$file\" exists: $!\n");
}

or

use strict;
use warnings;

# Properly decode source code, which is expected to be UTF-8.
# This allows non-ASCII characters in the source.
use utf8;

# Properly decode text received from STDIN.
# Properly encode text sent to STDOUT and STDERR.
use Win32 qw( );
my ( $enc_in, $enc_out, $enc_syscall );
BEGIN {
   $enc_input   = 'cp'.Win32::GetConsoleCP();
   $enc_output  = 'cp'.Win32::GetConsoleOutputCP();
   $enc_syscall = 'cp'.Win32::GetACP();

   binmode STDIN,  ":encoding($enc_input)";
   binmode STDOUT, ":encoding($enc_output)";
   binmode STDERR, ":encoding($enc_output)";
}

use Win32::Unicode::File qw( statW );

my $file = 'áéíóú.mov';

if (statW($file)) {
   print("$file exists\n");
}
elsif ($!{ENOENT}) {
   print("$file doesn't exist\n");
}
else {
   die("Can't determine if \"$file\" exists: $^E\n");
}

The latter isn't limited to paths containing characters of the machine's ANSI charset.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bash return value of test -e $file always returns false

Non ASCII Characters in filename are skipped

Why would Test-AzureName always return false?

Bash File existence test is always true

Formatting columns containing non-ascii characters

Will a UNICODE string just containing ASCII characters always be equal to the ASCII string?

Why does my getColor function always return false?

Why does "n&1 == 0" always return false?

Why does Thread.isInterrupted () always return false?

Why does ImageButton.onSetAlpha() return false always?

Open File with Non ASCII Characters

Why does info exists always return 0 when checkin for the existence of a canva object (Tcl/Tk 8.5)

Why is this find command not returning filenames containing non-ASCII characters only?

if (stdout=="E") always return false

Why does this return False

why does this test not return False when testing an integer

Why does False==False in [False] return True?

Why does std::remove for file always return -1?

Batch file with non-ASCII characters

Qt: Non ASCII characters in file names are replaced by '?'

How to fetch an URL containing non-ASCII characters (ą, ś ...) with Jsoup?

Why does json.dumps escape non-ascii characters with "\uxxxx"

Why does this always return 0?

How are bash variables containing a file path with non-ascii characters and spaces used as input for commands, cat, exiftool, etc.?

always return false when compare an existing string from a test file to the newly enterer object with parameter

Why this android function always return false?

Why isPasswordValid() function always return false?

Why Laravel Auth::check always return false?

Why bcrypt.compareSync() always return false?