使用Katalon Studio的Groovy脚本来实现自动化比较两个文件夹下同名的csv文件是否一致,并将比较结果输出到另一个文件中。要求不一致时定位到哪个文件的哪一行数据不一致
时间: 2023-12-03 20:42:06 浏览: 24
可以使用以下Groovy脚本来实现:
```groovy
import com.kms.katalon.core.util.KeywordUtil
// 定义文件夹路径
def folderPath1 = "path/to/folder1"
def folderPath2 = "path/to/folder2"
// 获取两个文件夹中的csv文件列表
def fileList1 = new File(folderPath1).listFiles({ file -> file.isFile() && file.getName().endsWith('.csv') })
def fileList2 = new File(folderPath2).listFiles({ file -> file.isFile() && file.getName().endsWith('.csv') })
// 比较两个文件夹中同名的csv文件
for (file1 in fileList1) {
def file2 = fileList2.find({ it.getName() == file1.getName() })
if (file2 != null) {
KeywordUtil.logInfo("Comparing file ${file1.getName()}...")
// 读取文件内容
def lines1 = new File(file1.getAbsolutePath()).readLines()
def lines2 = new File(file2.getAbsolutePath()).readLines()
// 比较每一行数据
def diffLineNums = []
for (int i = 0; i < lines1.size(); i++) {
if (lines1[i] != lines2[i]) {
diffLineNums.add(i + 1)
}
}
// 输出比较结果
if (diffLineNums.isEmpty()) {
KeywordUtil.logInfo("File ${file1.getName()} is identical.")
} else {
KeywordUtil.logInfo("File ${file1.getName()} is different.")
def resultFilePath = "path/to/result.csv"
def resultFile = new File(resultFilePath)
if (!resultFile.exists()) {
resultFile.createNewFile()
}
def resultLines = resultFile.readLines()
resultLines.add("File: ${file1.getName()}, Different lines: ${diffLineNums}")
resultFile.write(resultLines.join('\n'))
}
} else {
KeywordUtil.logWarning("File ${file1.getName()} does not exist in folder ${folderPath2}.")
}
}
```
在脚本中,我们使用了 `File` 类来获取文件夹中所有的csv文件列表,并通过 `readLines()` 方法读取文件内容。接着,我们比较每个文件的每一行数据,记录不一致的行号,并将比较结果输出到指定的文件中。运行脚本后,可以在指定的输出文件中查看比较结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)