使用Katalon Studio的Groovy脚本来实现自动化比较两个文件夹下同名文件是否一致
时间: 2023-11-28 16:50:47 浏览: 44
可以使用以下Groovy脚本来实现:
```groovy
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
def sourceFolder = "sourceFolder" // 源文件夹路径
def targetFolder = "targetFolder" // 目标文件夹路径
def sourcePath = Paths.get(sourceFolder)
def targetPath = Paths.get(targetFolder)
// 获取源文件夹下所有文件
def sourceFiles = Files.walk(sourcePath).filter { Files.isRegularFile(it) }.collect { it.toAbsolutePath().toString() }
// 遍历源文件夹下所有文件,与目标文件夹下同名文件进行比较
sourceFiles.each {
def relativePath = sourcePath.relativize(Paths.get(it))
def targetFilePath = targetPath.resolve(relativePath)
if (!Files.exists(targetFilePath)) {
println "文件 ${it} 在目标文件夹中不存在"
} else {
def sourceBytes = Files.readAllBytes(Paths.get(it))
def targetBytes = Files.readAllBytes(targetFilePath)
if (!Arrays.equals(sourceBytes, targetBytes)) {
println "文件 ${it} 与目标文件夹中的同名文件不一致"
}
}
}
```
使用时需要修改`sourceFolder`和`targetFolder`变量为对应的文件夹路径。该脚本会遍历源文件夹下所有文件,与目标文件夹下同名文件进行比较,如果存在不一致的文件,会输出提示信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)