[英]How to check all files in a directory except one file in Groovy language
I am trying to search a word in every file in a directory but I want to exclude my logfile.我正在尝试在目录中的每个文件中搜索一个单词,但我想排除我的日志文件。
My code is something like this我的代码是这样的
user input: search test C:\Users\Desktop\test\Groovy
My code我的代码
import static groovy.io.FileType.FILES
import java.text.SimpleDateFormat
def terminal_log = new File("terminal.log")
def terminal_log_path = terminal_log.getName()
def fas = ""
def file2_path = ""
def cmd = System.console().readLine 'Enter command: '
String[] csplice = cmd.split(" ");
if(csplice.length == 3){
def first_parameter = csplice[0]
def second_parameter = csplice[1]
def third_parameter = csplice[2]
if(first_parameter == "search"){
def file = new File(third_parameter)
if(file.exists()){
if(file.isDirectory()){
file.eachFile(FILES) { f ->
fas = "/"+f+"/"
File file2 = new File(fas)
file2_path = file2.getName()
if(!file2_path == terminal_log_path){
file2.eachLine{ line ->
if(line.contains(second_parameter)){
println "This file contains this word"
}
}
}
}
}else{
println "Not a directory"
}
}else{
println "Not exists"
}
}else{
println "Invalid command"
}
}else{
println "Invalid command"
}
This block here is not working这里的这个块不起作用
if(!file2_path == terminal_log_path){
Is there any documentation that I can read to exclude a specific file while checking every files in a directory?在检查目录中的每个文件时,我是否可以阅读任何文档以排除特定文件?
Many thanks非常感谢
EDIT: the directory of the user input has the logfile (terminal.log) terminal.log exists编辑:用户输入的目录有日志文件(terminal.log) terminal.log 存在
It should be:它应该是:
if (file2_path != terminal_log_path) {
...
}
or或者
if (!(file2_path == terminal_log_path)) {
...
}
Eg you can run the following code to see the result of applying the "Not" operator to a string in Groovy:例如,您可以运行以下代码来查看将“Not”运算符应用于 Groovy 中的字符串的结果:
def file2_path = "/i/am/path/"
println (!file2_path) // prints false file2_path is not an empty string
For more info, you can refer to the official Groovy doc on that topic:有关更多信息,您可以参考有关该主题的官方 Groovy 文档:
The "not" operator is represented with an exclamation mark (.) and inverts the result of the underlying boolean expression, In particular: it is possible to combine the not operator with the Groovy truth:
"not" 运算符用感叹号 (.) 表示,并反转底层 boolean 表达式的结果,特别是:可以将 not 运算符与 Groovy 事实相结合:
assert (!true) == false
assert (!'foo') == false
assert (!'') == true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.