fanticla has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks!
I am struggling with the following piece of code. My need: selecting a directory (possibly with UTF characters) and save a text file. This piece of code creates a folder and put inside it the file. How can I prevent the creation of the folder (this is the part I found on the web)? I just took away the "unless" part of the code, but if doing so, the script is no longer able to write in a folder with UTF characters! What am I missing? Thanks. Cla
use Tk; use strict; use warnings; 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 $mw = MainWindow->new; $mw->title('Menu'); my $menubar = $mw->Menu(-type => 'menubar'); $mw->configure(-menu => $menubar); my $mfile = $menubar->cascade(-label => '~File', -tearoff => 0); $mfile->command(-label => '~Save', -command => \&save_file4); my $exit = $mw->Button(-text => 'Exit', -command => [$mw => 'destroy']); $exit->pack; my $types = [ ['HTML files', '.txt'], ['All Files', '*'],]; MainLoop; sub save_file4 { my $save = $mw->getSaveFile(-filetypes => $types, -initialfile => 'test', -defaultextension => '.txt'); unless ( Win32::CreateDirectory($save) ) { my $err = $^E; if ( $err == ERROR_ALREADY_EXISTS ) { warn "Directory exists, no problem\n"; } else { die Win32::FormatMessage($^E); } } my $ansi_path = Win32::GetANSIPathName($save); warn "$ansi_path\n"; my $file = catfile($ansi_path, 'test.txt'); open(OUT,">$file") or die $!; print OUT $file; close OUT; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: path encoding folder
by moritz (Cardinal) on Nov 19, 2010 at 15:11 UTC | |
by fanticla (Scribe) on Nov 19, 2010 at 15:23 UTC | |
|
Re: path encoding folder
by ikegami (Patriarch) on Nov 19, 2010 at 15:43 UTC | |
by fanticla (Scribe) on Nov 19, 2010 at 16:23 UTC | |
by ikegami (Patriarch) on Nov 19, 2010 at 19:31 UTC |