Search files containing multiple keywords MOCA/Groovy
If you are a Blue Yonder WMS consultant or developer, I am certain you have gone through the situation where you need to find files containing multiple words. For example, if you want to search for a file in which specific policy (polcod and polval) is used. here is the platform independent MOCA script to get this problem resolved. :)
keywords_list_to_find → comma seperated list of keywords to be searched
path → main dir path under which files have to be searched
search_in_sequence → search for keywords in provided sequence?
readLimit → buffer size while reading files
publish data
where keywords_list_to_find = @keywords_list_to_find
and path = nvl(@path,@@LESDIR||'\src')
and search_in_sequence = nvl(@search_in_sequence,false)
and readLimit = nvl(@readLimit,'99999')
|
[[
path = parsePath(path);
readAheadLimit = Integer.parseInt(readLimit!=null||readLimit>0?readLimit:'99999')
File[] files = new File(path).listFiles();
SimpleResults simpleResults = new SimpleResults();
simpleResults.addColumn("filepath",MocaType.STRING);
found_res= getFiles(files,simpleResults);
[found_res:found_res]private String parsePath(String filePath){
retFilePath = filePath
if(filePath.contains("@@")){
envVariable = filePath.substring(filePath.indexOf("@@"),filePath.indexOf("\\"))
res = moca.executeCommand("publish data where x = "+envVariable)
if(res.next()){
envVarVal = res.getString("x");
}
retFilePath = filePath.replace(envVariable,envVarVal)
}
return retFilePath
}private SimpleResults getFiles(File[] files ,SimpleResults simpleResults) {
for (File file : files) {
if (file.isDirectory()) {
getFiles(file.listFiles(),simpleResults);
} else {
filepath = file.getPath()
if(fileContainsKeywords(filepath,keywords_list_to_find)){
simpleResults.addRow();
simpleResults.setValue("filepath",filepath);
}
}
}
return simpleResults;
}private boolean fileContainsKeywords(String pathname,String searchList) {
String[] keywordList = searchList.split(",");
boolean found = false;
File file = new File(pathname);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
bufferedReader.mark(readAheadLimit);
String current_line =null;
for(String keyword : keywordList){
while((current_line=bufferedReader.readLine())!=null){
if(current_line.contains(keyword)){
found = true;
if(!search_in_sequence)
bufferedReader.reset();
break;
}
else{
found = false;
}
}
if(found){
continue;
}
else
break;
}
return found;
}
]]
|
publish data combination
where res = @found_res
let say you want to search for LES commands using policy USR-DEFAULT,ORD_NOTE-UC_PAKNOT
or in LES files I want to search for files containing “ZPL_LABEL.zpl.xml” and a specific barcode “S20000000A66”, this would parse LESDIR\files\ and bring all files covering search criteria.
It will parse directories recursively so response time depends on data exists in given directory.