Forum Discussion

indraniria's avatar
indraniria
Contributor
2 years ago

How to find a file containing a particular substring from a list of files in a folder using groovy?

I have a list of files inside a folder. From these files , I have to find a file that is containing a particular transaction (a substring). How do I find that particular file using groovy scripting?
  • ChrisAdams's avatar
    2 years ago

    Hi,

     

    The code below might not be the most elegant of efficient, but it works....

     

    def filesToLookAt = findStringInFilesInFolder("c:\\Temp", "Time");
    
    log.info(filesToLookAt);
    
    return filesToLookAt;
    
    
    
    def findStringInFilesInFolder(folderName, searchString) {
    
    	log.info("Searching for '${searchString}' in '${folderName}'.");
    
    	def filesContainingSearchString = [];
    
         // Let's add the file names into a List for later...
    	def fileNames = findFilesInFolder(folderName);
    	
    	log.info(fileNames);
    
    	fileNames.each { file ->
    		if (searchInFile(file, searchString)) {
    			filesContainingSearchString.add(file)
    		}
    	}
    
    	return filesContainingSearchString;
    }
    
    def findFilesInFolder(folderName) {
    
    	def fileNames = [];
    
    	new File(folderName).eachFile() {  file->
    		if (file.isFile()) {
    			fileNames.add( file.getAbsolutePath() )
    		}
          }
    
          return fileNames;
    }
    
    def searchInFile(fileName, searchValue) {
    
    	def stringFound = false;
    	
    	log.info("Checking ${fileName}.");
    
    	// Only search within text files.
    	if( fileName.contains(".txt") ||
    		fileName.contains(".csv") ||
    		fileName.contains(".xml") ||
    		fileName.contains(".htm") ) {
    
    		new File(fileName).eachLine {  line->
    			if (line.contains(searchValue)) {
    				stringFound = true
    			}
    		}
    			
    	} else {
    		log.info("File '${fileName}' is not a type to search in.")
    	}
    
    	return stringFound;
    	
    }
    
  • ChrisAdams's avatar
    2 years ago

    Hi,

     

    Here's v2 of the script which returns the first file that the search string is in.  It uses a for loop to iterate over the file names.  When the string is found, it stops and returns the file it was found in.

    This might help you with your other question about breaking out of loops.

    def fileToLookAt = findStringInFilesInFolder("c:\\Temp", "String to search for");
    
    log.info(fileToLookAt);
    
    return fileToLookAt;
    
    
    
    def findStringInFilesInFolder(folderName, searchString) {
    
    	log.info("Searching for '${searchString}' in '${folderName}'.");
    
         // Let's add the file names into a List for later...
    	def fileNames = findFilesInFolder(folderName);
    	
    	log.info(fileNames);
    
    	def numOfFiles = fileNames.size();
    	def found = false;
    	int count;
    
    	// Use a For loop so we can break early...
    	for(count = 0; count < numOfFiles; count++) {
    
    		found = searchInFile(fileNames[count], searchString);
    
    		if (found) {
    			log.info("Cool!  Found ${searchString} in ${fileNames[count]}, we only searched ${count + 1} files, instead of ${numOfFiles}.");
    			break;
    		}
    	}
    
    	if (found) {
    		return fileNames[count];
    	} else {
    		return "";
    	}
    	
    }
    
    def findFilesInFolder(folderName) {
    
    	def fileNames = [];
    
    	new File(folderName).eachFile() {  file->
    		if (file.isFile()) {
    			fileNames.add( file.getAbsolutePath() )
    		}
          }
    
          return fileNames;
    }
    
    def searchInFile(fileName, searchValue) {
    
    	def stringFound = false;
    	
    	log.info("Checking ${fileName}.");
    
    	// Only search within text files.
    	if( fileName.contains(".txt") ||
    		fileName.contains(".csv") ||
    		fileName.contains(".xml") ||
    		fileName.contains(".htm") ) {
    
    		String fileContents = new File(fileName).getText('UTF-8');
    
    		if (fileContents.contains(searchValue)) {
    				stringFound = true
    		}
    			
    	} else {
    		log.info("File '${fileName}' is not a type to search in.")
    	}
    
    	return stringFound;
    	
    }