Use Perl to implement a batch file transcoding Tool

Source: Internet
Author: User

A few days ago in Look At The ultimate_toolbox source code, from the codeproject down a compilation, a bunch of errors, found that because the compiler can not identify the ISO-8859-1 encoding, misunderstanding of the relevant characters caused, so I started to prepare the conversion program source file encoding immediately. I made a Google copy and two small software, but they didn't have the batch conversion function, and this function does not support the conversion from the ISO-8859-1 to the UTF-8, so think of the iconv library of the function that previously converted the UTF-8 and gb2312 under FreeBSD
I know that iconv is not only a library, but also an application with the same name can convert files in two types of encoding. Using this iconv program with the script re-use of the redirection function should be able to easily complete the bulk conversion of file encoding.

In the process of writing a script, you are afraid of unexpected operations in the future. Therefore, you have not redirected the transcoded file to the source file, but specified another location, at the same time, in order to facilitate the compilation of the entire ultimate_toolbox code in the future, the files that do not need to be encoded in the project directory are all copied to the new location as is, such a recursive traversal directory, the tool for bulk conversion of file encoding was born.

The Perl source program is attached. If iconv has been transplanted in Windows, it can run in Windows with activperl.

 

#! /Cygdrive/C/perl/bin/perl
######################################## ###############
# Purpose: Convert the encoding of program source files in a folder in batches, and keep the remaining files unchanged.
#
# Usage: $./change_codepage.pl> log.txt
#
# Author: changhailong
#
# E-mail: hailongchang165210@gmail.com
######################################## ###############

Use File: Find;
Use File: copy;
Use File: path/make_path /;

# Specify the directory where the codePage source file needs to be converted.
$ Source_dir = "D:/temp_code/ultimate_tools ";

# Specify the target directory here
$ Destination_dir = "D:/temp_code/PP ";

Find (/& traverse, $ source_dir );

Sub traverse ()
{
My $ file = $ file: Find: Name;

My $ df = $ file;
$ Df = ~ S // Q $ source_dir/e/$ destination_dir/g;

Print ("Source: $ File/N ");
Print ("Destination: $ _/N ");
If (-d $ _)
{
If (-e $ DF) # If the directory already exists, continue recursive Traversal
{
Print ("the directory has been exist: $ DF/N ");
Last;
}
Else # create a directory if the directory does not exist
{
Print ("it is a directory, create: $ DF/N ");
File: Path: make_path ($ DF );
}
}
Else
{
If (//. cpp $ |/. h $ |/. C $ |/. INL $/I) # use a regular expression to specify the file type to be converted and encoded.
{
My $ cmd = "iconv-F ISO-8859-1-T UTF-8 </" $ File/">/" $ DF/"; # convert a ISO-8859-1 to a UTF-8
Print "$ cmd/N ";
System $ cmd;
}
Else # If you do not need to convert the encoding file type, copy it to the new location.
{
Print ("Copy $ file to $ DF/N ");
Copy ($ file, $ DF) or die "Copy failed: $! /N ";
}
}

Print ("/n ");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.