简体   繁体   English

如何检查目录中除 Groovy 语言中的一个文件之外的所有文件

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

相关问题 如何根据Groovy中目录的大小对所有文件排序? - how to sort all the file according to size in directory in groovy? 如何使用Groovy读取文件夹中的所有文件并替换文件中的模式 - How read all files in the folder and replace the pattern in file using Groovy 如何从 groovy (Jenkins) 中的两个文件中写入一个文件? - How to write one file from two files in groovy (Jenkins)? 如何使用Groovy脚本从给定目录中获取所有* .sln文件? - How to get all the *.sln files from a given directory using Groovy script? 如何在Groovy中检查文件中的UTF-8 BOM? - How to check for UTF-8 BOM in files in Groovy? Jenkins Groovy Pipeline-将目录中的所有文本文件拉入列表 - Jenkins Groovy Pipeline - Pull all text files from a directory into a List 如何在groovy语言中逐个添加项目到数组 - How to add items to an array one by one in groovy language Groovy和Groovy ++是两种语言还是一种语言? - Are Groovy and Groovy++ two languages or one language? Groovy:当我加载资源文件时,没有这样的文件或目录 - Groovy : No such file or directory, when I load resource files Groovy(文件IO):查找所有文件并返回所有文件 - Groovy方式 - Groovy (File IO): find all files and return all files - the Groovy way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM