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