Common those file operation functions are not supported, so in order to achieve the goal, need a variety of methods to cooperate, should be less convenient than other languages.
I just want to see if Perl is the right thing to do, and then toss it back.
Creation of documents:
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Use Win32;
- Use UTF8;
- Use Encode;
- #接受unicode传参
- Win32::CreateFile("W32createfile test");
attribute: Successfully returns true but does not return a file
handle
creates the FILE and returns a true value on success.
Check $^e On Failure for extended error information.
function: $hObject = Createfilew ($swPath, $uAccess, $uShare, $pSecAttr, $uCreate
, $uFlags, $hModel)
$hObject can return a handle to a file object
The encoded format of the incoming file path is: Utf16-le, which must end with \x00, example (the code is saved in UTF8 format):
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Use Win32API::File qw(: All);
- Use UTF8;
- Use Encode;
- $str="Wen tes?t.txt\x00";
- $hobject =createfilew (Encode Utf16-le" Span style= "color: #339933;" >, $str ) generic_ Write0 [] Open_always,< Span style= "color: #cc66cc;" >0,0;
Establishment of a directory
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Use Win32;
- Use UTF8;
- Win32::createdirectory("Dir test");
Enumeration of files
in the case of Unicode characters, the File::find module and IO::D IR module can only output short file names.
temporarily use the cmd/u Dir method to output the file list (depressed bar, temporarily did not find the perfect operation of the built-in module)
http://www.perlmonks.org/?node_id=536223
Copy a file within a folder (the file name contains Unicode characters)
If you first get the short name of the file and then copy it, the target file name becomes a short name.
then temporarily use cmd/u mode to get the file list, and then copyfilew to copy:
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Use Win32API:: "File qw': All ';
- Use Encode;
- Use UTF8;
- My $src=encode(' GBK ','. \ \ Test directory ');
- My $dst='. \\Target ';
- #该目录只有一层, the/s switch is to list the full path
- My $all=' cmd/u/C dir/s/b \ ' $src\ ';
- My $fn;
- foreach split (/\x0d\x00\x0a\x00/, $all ) {
- $FN = Encode GBK ' decode< Span style= "color: #009900;" > ( ' Utf16-le ' ,$_) ) " \n "
- @xrr=split(/\x5c\x00/, $_);
- Copyfilew(
- $_ ." \x00 ",
- Encode(' Utf-16le ', decode(' UTF8 ', "$dst\ \")). $XRR[$ #xrr]." \x00 ",
- 1
- );
- print "$^e\ n" if ($^e);
- }
- <STDIN>;
Use split $all truncate the UTF-16LE character paragraph correctly, the delimiter is 0d 0a xx
Reference Enumeration Script
If you use BaseName () to split the path, you will also encounter 00 ignored issues, ' \ \ ' U16le
encoding is 5C 00, but basename only by 5C truncation, the remaining 00 caused the processing garbled.
The
second parameter of test basename is set to "\x5c\x00" and does not solve this problem
manually remove the starting point
first turn to GBK, then get basename, then Utf-16le
2014-12-12 Note This method loses Unicode characters in the case of Longpath
you can consider turning into UTF-8, which is a little bit around anyway
.
get it yourself with regular expressions
@xrr =split (/\x5c\x00/, $_);
Copyfilew When copying a file, add \x00 as a string terminator at the end
otherwise all sorts of problems =_=
Determine if the file exists:
method One: First to the short name and then judge, do not repeat
method Two: Slag method, using Createfilew test to establish the same name file, see if there is a conflict
Rename:
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Movefilew(
- Encode ( Decode ( " UTF8 ' , $F ) ) "\x00" ,
- Encode(' Utf-16le ', decode(' UTF8 ',$newname))." \x00 "
- );
Get date information for a file:
the case of a common file name
http://stackoverflow.com/questions/1839877/
How-can-i-get-a-files-modification-date-in-ddmmyy-format-in-perl
case with a file name containing Unicode characters
http://www.perlmonks.org/?node_id=741797
How to
stat a file with a Unicode (utf16-le) filename in Windows?
the method is to get the file handle through Createfilew, and then use Osfhandleopen to get a generic file handle object and pass
in the state
The
other is to go to the short name and then get the date, but this method is very inefficient when dealing with a large amount of files. Methods in the previous Perlmonks
Code:[Select all] [Expand/shrink] [Download] (example.pl)
- Use UTF8;
- Use Encode;
- Use Win32;
- $filename=' d:\ Test catalog \ Dong Zhe? 01. The sword is like a rainbow. [Zhen hu Hu].mp3 ';
- $filename=win32::getshortpathname($filename);
- My $mtime = (stat $filename)[9];
- My ( $sec , $min , $hour , $mday , $mon , $year , $wday , $yday , $ISDST ) = localtime $mtime Span style= "color: #009900;" >)
- $year+ =1900;
- $mon+ =1;
- Print "$year-$mon-$mday\ n";
- <STDIN>;
Windows system Unicode File name operation (new, rename, enumerate, copy) all tips