Batch Rename file tool implemented in Go language

Source: Internet
Author: User

This morning want to send files to the network, but also want to change the file name to change the name of the MD5, a change is more troublesome.

has been studying the go language recently, the language is relatively concise, can be compiled into executable programs, so in the go language wrote a gadget called Md5ren.

The function implemented is to execute the command Md5ren in the folder to be modified, the goal is to change all the files and folders under the folder to the MD5 value of the original name, and if it is a file, retain the suffix of the original file.

The functions of the go language used are:

    • List all files under a folder
    • Determine the file type, whether it is a file or a folder
    • Write a file
    • Generates the MD5 value of a string
list all files under a folder
Files, err: = Ioutil. ReadDir (dir)
Determine file type

Through Ioutil. Readdir returns the result of the FileInfo type of data, FileInfo has a Isdir method that can determine whether the folder.

Write a file

Write files can use Ioutil. WriteFile to implement the code as follows:

Ioutil. WriteFile (Renlogfilename, []byte0666)
Computes the MD5 value of a string
1 string string {2     hasher: = MD5. New ()3     hasher. Write ([]byte(text))4     return  hex. Encodetostring (Hasher. Sum (nil))5 }

The full code is in GitHub.

Batch Rename file tool implemented in Go language

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.