Let’s get start on building our file renaming tool. Before diving into the code, let’s lay out the requirements:

让我们开始构建文件重命名工具。 在深入研究代码之前,让我们对需求进行布局:

  • Ability to specify the target folder 能够指定目标文件夹
  • Ability to scan all files in subfolders能够扫描子文件夹中的所有文件
  • Ability to include or exclude files based on name or extension能够根据名称或扩展名包含或排除文件
main.gocmd
main.gocmd
main()
main()
cmd.RootCmd.Execute()
go run main.go
go run main.go
Image for post
Auto-generated help message on root command
在root命令上自动生成的帮助消息
子指令Sub command
renamefilezy rename
renamefilezy rename
Basic rename command
基本重命名命令
cobra.ExactArgs(1)
cobra.ExactArgs(1)
renameCmd.Flags().StringVarP(&folder, "folder", "f", "./", "Target folder to be scanned")
-frenameCmd.//c/folderfilezy rename [filename] -f /c/folder
-frenameCmd.//c/folderfilezy rename [filename] -f /c/folder
从文件夹读取文件 Read files from folder

After specifying a flag to read the folder input, let’s implement the logic to read files from the folder.

在指定一个标志以读取文件夹输入后,让我们实现从文件夹中读取文件的逻辑。

Read files from a folder
从文件夹读取文件
os.Open(folder)Readdir(-1)files
os.Open(folder)Readdir(-1)files
从子文件夹递归读取文件 Read files from subfolder recursively
path/filepath
path/filepath
Read files from sub folder recursively
从子文件夹递归读取文件

The walk function walks the file tree starting from the folder given (as root) into each file or directory in the tree. Note that it can be quite slow when it comes to a large directory.

巡视功能将文件树从给定的文件夹(作为根目录)开始移入树中的每个文件或目录。 请注意,对于大型目录,它可能会非常慢。

添加过滤器 Adding filter

Not all of the files scanned shall be renamed and that’s why it’s advisable to add a filter to include or exclude files based on the file name or extension. There are several filters that I can think of, for example based on prefix, suffix, regex pattern and extension.

并非所有扫描的文件都应重命名,因此,建议根据文件名或扩展名添加过滤器以包括或排除文件。 我可以想到几种过滤器,例如基于前缀,后缀,正则表达式模式和扩展名的过滤器。

To ease the process, it will be easier that we can store the file name in separate fields such as base file name and extension. Again, with the use of flags, we can get the input of prefix, suffix, regex or extension.

为了简化此过程,将文件名存储在单独的字段(例如基本文件名和扩展名)中会更加容易。 同样,使用标志,我们可以获得前缀,后缀,正则表达式或扩展名的输入。

Filter files based on file name
根据文件名过滤文件
stringsregexp
stringsregexp
文件编号 Numbering of the files

As we are renaming the files in batch where they can potentially be overwritten, we should come up with a mechanism to add an auto-increment number as suffix to the file name.

当我们批量重命名文件时,它们有可能被覆盖,因此,我们应该想出一种机制,在文件名中添加一个自动递增的数字作为后缀。

Besides that, we should keep the sequence of the files the same as before. To achieve this, simply appending number will mess up the sequence of the files. To give you an example, assuming that there are 20 files to be renamed and the names will be as such: file-1.jpg, file-2.jpg, … … file-10.jpg, file-11.jpg… … file-20.jpg. As a result, file-10.jpg will be sorted before file-2.jpg.

除此之外,我们应该保持文件顺序与以前相同。 为了达到这个目的,只需加上数字就会弄乱文件的顺序。 举一个例子,假设有20个文件被重命名,名称将是这样:file-1.jpg,file-2.jpg,………file-10.jpg,file-11.jpg…………文件20.jpg。 结果,file-10.jpg将在file-2.jpg之前排序。

To avoid the sequence being reordered, we can add zero in front of the numbers to standardize the number of digits of the suffix. In the code excerpt below, we create two helper functions: to calculate the number of digits of the total number of files and to get the auto-incremental suffix number.

为避免对序列进行重新排序,我们可以在数字前面添加零以标准化后缀的位数。 在下面的代码摘录中,我们创建两个帮助器函数:计算文件总数的位数并获取自动递增的后缀数。

Getting the auto-incremental suffix
获取自动递增的后缀
重命名文件Renaming the files

With the old file name and newly built file name, we can call the following function to rename the files.

使用旧文件名和新建文件名,我们可以调用以下函数来重命名文件。

os.Rename(<old file name>, <new file name>)

Do note that if there is existing file name that coincides with the new file name, then the existing file will be replaced.

请注意,如果现有文件名与新文件名一致,则将替换现有文件。

构建并运行 Build and run
go build
go build