Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 582001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:38:24+00:00 2026-05-13T14:38:24+00:00

I need to check whether a Unicode directory exists in Perl. I am using

  • 0

I need to check whether a Unicode directory exists in Perl. I am using Windows XP and Perl Camelbox 5.10.0.

If I try to create a directory (like Sinan suggested here stackoverflow.com/questions/2184726) that already exists the program dies.

Unfortunately if ( !-d $dir_name ) { # create directory $dir_name } doesn’t seem to recognize Unicode directories or I am doing something completely stupid. I tried to encode the directory name before checking it, but the result is the same.

How do I check for the existance of a Unicode directory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:38:25+00:00Added an answer on May 13, 2026 at 2:38 pm

    When answering your earlier question, I forgot that Win32.pm provides a decent interface. I will go back and that answer. However, for your immediate problem, what you need to do is not to automatically die when the CreateDirectory call fails, but to check the error code. If the error code is 0xb7 (ERROR_ALREADY_EXISTS), you go on your merry way.

    The problem is that it is hard to go on your merry way using Perl functions when you have a Unicode file name. The solution is to use Win32::GetANSIPath (just keep an eye on the full length of the path):

    #!/usr/bin/perl
    
    use strict; use warnings;
    use utf8;
    
    use Encode qw( encode );
    use File::Slurp;
    use File::Spec::Functions qw( catfile );
    use Win32;
    use Win32::API;
    
    use constant ERROR_ALREADY_EXISTS => 0xb7;
    
    my $dir_name = 'Волгогра́д';
    
    unless ( Win32::CreateDirectory($dir_name) ) {
        my $err = $^E;
        if ( $err == ERROR_ALREADY_EXISTS ) {
            warn "Directory exists, no problem\n";
        }
        else {
            die Win32::FormatMessage($^E);
        }
    }
    
    my $ansi_path = Win32::GetANSIPathName($dir_name);
    warn "$ansi_path\n";
    

    Oh, and, good luck deleting that directory.

    In a serious vein, though, the whole Windows Unicode file operations thing is a bit of a mess.

    As far as I understand these things, you need the ANSI path name if you want to be able to use Perl functions such as open to work with paths containing Unicode characters. E.g.:

    my $file = catfile($dir_name, 'test.txt');
    
    open my $fh, '>', $file
        or die "cannot create '$file': $!";
    

    will fail whereas

    my $file = catfile($ansi_path, 'test.txt');
    
    open my $fh, '>', $file
        or die "cannot create '$file': $!";
    

    will succeed (at least on my system). You do not need the ANSI paths if you are going to only use Win32 API functions to deal with files (and that might be easier in your case). There are a bunch of modules to help you with the latter on CPAN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.