[英]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.