Skip to content

Instantly share code, notes, and snippets.

@sfdcale
Last active December 2, 2018 23:35
Show Gist options
  • Save sfdcale/6ef17cc2409b83dda9fefdce9810995b to your computer and use it in GitHub Desktop.
Save sfdcale/6ef17cc2409b83dda9fefdce9810995b to your computer and use it in GitHub Desktop.
This script finds all the files in which specific fields are used.
package com.RollUpSummaryFieldFinder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MainClass {
static Map<String, Set<String>> matchingFileSet = new HashMap<>();
static List<String> fieldNames = new ArrayList<String>();
static Set<String> excludeFolderNamesSet = new HashSet<String>();
public static void main(String[] args){
File folder = new File("/home/ram/JohnDoe/DEV_SANDBOX/src");
fieldNames.add("Field_One__c");
fieldNames.add("Field_Two__c");
excludeFolderNamesSet.add("profiles");
excludeFolderNamesSet.add("permissionsets");
excludeFolderNamesSet.add("layouts");
excludeFolderNamesSet.add("objecttranslations");
excludeFolderNamesSet.add("letterhead");
excludeFolderNamesSet.add("staticresources");
excludeFolderNamesSet.add("wave");
try{code
searchFiles(folder);
}catch(Exception e){
System.out.println(e.getMessage());
}
for(String fileName: matchingFileSet.keySet()){
System.out.println(fileName + " uses the below fields: ");
for(String fieldName: matchingFileSet.get(fileName)){
System.out.println(" " + fieldName);
}
}
}
public static void searchFiles(File folderName) throws FileNotFoundException,IOException{
boolean printedCurrentFolder = false;
for (File fileEntry : folderName.listFiles()) {
if(!printedCurrentFolder){
System.out.println("Searching files in folder "+ folderName.getName());
printedCurrentFolder = true;
}
boolean invalidFile = fileEntry.getName().endsWith("-meta.xml");
invalidFile = invalidFile || fileEntry.getName().endsWith(".profile");
invalidFile = invalidFile || fileEntry.getName().endsWith(".png");
invalidFile = invalidFile || fileEntry.getName().endsWith(".quickAction");
invalidFile = invalidFile || fileEntry.getName().endsWith(".queue");
if(!invalidFile && fileEntry.isFile()){
System.out.println("Searching file "+ fileEntry.getName());
FileReader fileReader = new FileReader(fileEntry);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
for(String fieldName : fieldNames){
if(line.contains(fieldName)){
if(matchingFileSet.containsKey(fileEntry.getName())){
matchingFileSet.get(fileEntry.getName()).add(fieldName);
}else{
matchingFileSet.put(fileEntry.getName(),new HashSet<String>());
matchingFileSet.get(fileEntry.getName()).add(fieldName);
}
}
}
}
fileReader.close();
}
}
File[] childFolders = getChildDirectories(folderName);
for (int i = 0; i < childFolders.length; i++) {
if(!excludeFolderNamesSet.contains(childFolders[i].getName().toLowerCase())){
searchFiles(childFolders[i]);
}
}
}
public static File[] getChildDirectories(File folderName){
List<File> subFolders = new ArrayList<File>();
String[] names = folderName.list();
for(String name : names){
if (new File(folderName.getAbsolutePath() + "/" + name).isDirectory()){
subFolders.add(new File(folderName.getAbsolutePath() + "/" + name));
}
}
return subFolders.toArray(new File[subFolders.size()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment