[英]How to check all files in a directory except one file in Groovy language
我正在尝试在目录中的每个文件中搜索一个单词,但我想排除我的日志文件。
我的代码是这样的
user input: search test C:\Users\Desktop\test\Groovy
我的代码
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"
}
这里的这个块不起作用
if(!file2_path == terminal_log_path){
在检查目录中的每个文件时,我是否可以阅读任何文档以排除特定文件?
非常感谢
编辑:用户输入的目录有日志文件(terminal.log) terminal.log 存在
它应该是:
if (file2_path != terminal_log_path) {
...
}
或者
if (!(file2_path == terminal_log_path)) {
...
}
例如,您可以运行以下代码来查看将“Not”运算符应用于 Groovy 中的字符串的结果:
def file2_path = "/i/am/path/"
println (!file2_path) // prints false file2_path is not an empty string
有关更多信息,您可以参考有关该主题的官方 Groovy 文档:
"not" 运算符用感叹号 (.) 表示,并反转底层 boolean 表达式的结果,特别是:可以将 not 运算符与 Groovy 事实相结合:
assert (!true) == false
assert (!'foo') == false
assert (!'') == true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.