编写Jenkins Groovy脚本文件以获取给定目录(包括子目录)中的所有.NET项目解决方案文件(*.sln
)。
首先,我尝试获取给定路径中的目录列表。我使用下面的代码工作正常:
File folder = new File("D:\\Data");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
println("File " + listOfFiles[i].getName());
}
else if (listOfFiles[i].isDirectory()) {
println("Directory " + listOfFiles[i].getName());
}
}
2)当修改下面的代码以列出给定路径中的所有解决方案文件时,我收到错误:
File folder = new File("D:\\Data");
File[] listOfFiles = folder.listFiles();
Collection files = FileUtils.listFiles(
folder,
new RegexFileFilter("^(*.sln)"),
DirectoryFileFilter.DIRECTORY
);
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
println("File " + files[i].getName());
}
else if (files[i].isDirectory()) {
println("Directory " + files[i].getName());
}
}
错误详情:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: unable to resolve class RegexFileFilter
@ line 5, column 3.
new RegexFileFilter("^(*.sln)"),
^
有谁可以帮我解决这个问题,以获取给定路径中的所有解决方案文件(包括子目录)
提前致谢!!
答案 0 :(得分:1)
Simples解决方案是使用FileNameFinder,如下所示:
var COL = 2;
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
sheet.insertColumnAfter(COL);
// data of the new column
var dataNewCol = [];
for (var i = 0; i < data.length; i++) {
var response = getData(data[i][1]);
dataNewCol[i] = [];
dataNewCol[i][0] = response;
}
// Set Values in the newly added column (COL+1)
sheet.getRange(1,COL+1,sheet.getLastRow(),1).setValues(dataNewCol);
}
function getData(user) {
var url = "https://api.lootbox.eu/pc/eu/" + user + "/profile";
var response = UrlFetchApp.fetch(url, {
contentType: "application/json",
headers: {
Accept: "application/json"
}
});
Logger.log(response);
// return data
return response;
}
答案 1 :(得分:0)
这应该这样做
import static groovy.io.FileType.*
def startDir = new File("D:\\Data")
startDir.eachFileRecurse(FILES) {
if (it.name.endsWith('.sln')) {
println it
}
}