During the experiment, the folder name needs to be manually modified every time, which makes too much trouble for yourself. In order to get rid of the annoyance of manual modification, therefore, it is a good idea to use programs to modify folder names in batches. In order to achieve this goal, we naturally need to select a suitable scripting language. At present, we are very interested in Ruby, so "Interest + demand ", the birth of a Ruby program is not too ugly (self-thinking ).
(1) Functional Requirements
First, let's talk about my requirements: There are 50 sub-folders under the root directory (marked as LDAModel) of the program running (names: 21-70, respectively. Why not start from 1 ?), Each sub-folder has several sub-folders whose names are classified into 20 and 20 (I), as shown in:
The folder named 20 does not necessarily exist. If it does not exist, it will convert one of 20 (I) to 20 each time the program runs.
In addition, the data in the 20 (I) folder may be incorrect. If the number of files in the folder is smaller than 6, it cannot be used. Therefore, you should delete these folders directly!
(2) Code and description
The following shows the code that meets the above requirements:
Require queryFolders = Dir. entries (folder). delete_if {| e = ~ /^ \ .. */| QueryFolders. each {| e | tFolder = Dir. entries (e). delete_if {| e = ~ /^ \ .. */TFolder. each {| model | FileUtils. rm_rf () Dir. entries (). delete_if {| e1 | e = ~ /^ \ .. */}. Length () <6 next tFolder. include? (UnRenameFolder = tFolder. each {| e = ~ /20 \ (d {1, 3} \)/}). length> File. rename (, rename (File. dirname ())View Code
It should be noted that the Code only passes the test in the Mac system. The feasibility of other environments remains to be proved.
Next we will briefly analyze the above Code
This part of the code consists of three parts: The first part is the reference library FILEUTILS, the second part is the definition function, and the third part is the call function.
rename(File.dirname())
First, let's talk about the third part (the last line of the Code). The most important thing to note is the directory where the current file is located, rather than the currently running directory. If you need to obtain the directory currently running, you need to use it (friends familiar with Linux should be familiar with it, pwd oh, not password Oh ~~ Hey)
Next, let's look back at the function definition section. To implement the above functions, first find the sub-folders at the first layer (I .e. the folders and 23 in the figure ), and remove the hidden content and common files (what we need is folders ). Therefore
queryFolders = Dir.entries( folder ).delete_if {|e| e=~ /^\..*/ || !File.directory?(e)}
Here, we use regular expressions to filter out hidden files (some hidden files may interfere with our normal operations in the Mac system, but does this method work in Windows? This is my doubt), using File. directory? Filter out common documents. I have to lament that Ruby is flexible as a dynamic language. I want to think of objects as strings and strings, and think of objects as files. As a brother surnamed Zhang said N years ago, "one line of code is done !", The old man is honest.
The next step is traversal, and each solves the problem.
Next, determine whether there are more than 6 files in each folder. If not, delete the files. There are two types of delete folders, the other is the method used in this article-the former can only delete empty folders, while the latter is more lethal and can be directly rooted in the root, which is exactly the way we need.
I believe many people have noticed that when the FileUtils. rm_rf and Dir. entries methods are called, if the model is a file during parameter filling, why not call it directly?
FileUtils. rm_rf (model) and Dir. entries (model)
To be honest, if you use Java, you only need to do so. But in Ruby, after many attempts, I always get
No such file directory
If Dir. pwd is used in this row, you can clearly see that the current running directory is LDAModel (that is, the parent directory of). In this case, Dir. entries (model) is used, which is equivalent
FileUtils.rm_rf("LDA/20")
Dir.entries("LDA/20")
Because the two paths do not exist, the correct code cannot be executed.
Although the problem has been found, I still have many questions about the Ruby operating mechanism. I hope you will be enlightened by me ~~~
It should be noted that Find can be used to traverse files in many books. In fact, this trick really does not meet the basic requirements of the author, because Find traverses all the files and directories in the folder (it seems that depth is preferred, but cannot be remembered ), therefore, I am directly ignoring it!