Find/Search Hard Coded string in Files under Directories and Sub-Directories

Many times we hard code some string in files which could lead to Production issue.We seems to ignore this minor changes.Below is the code which could help to find the File/Files  in which such hard code string is present as well as shows the line in which it occurs.

 

bat_file

Steps:

  1. Click on bat file.
  2. Insert the Source folder path ( where java files are present ) and press Enter
  3. Go to C://logs//searchString.log to check the logs

In log files , we will be able to see the below content :

 

log_files

 

Code:

  1. Create a mavenized Project and add jar file dependencies such as log4j , commons-logging and commons-io in pom.xml
  2. Pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.subhasish</groupId>
<artifactId>SearchString</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url&gt;
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.subhasish.ScanFiles</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>SEARCHSTRING</finalName>
</build>
</project>

3) ScanFiles.java

package com.subhasish

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.log4j.Logger;

public class ScanFiles {

private static final Logger logger = ScanFilesLogger.getLogger(ScanFiles.class);

private static void searchFiles(String folderPath, String searchString) throws IOException {
File folder = new File(folderPath);
List<File> files = (List<File>) FileUtils.listFiles(folder, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
if (folder.isDirectory()) {
File[] fList = folder.listFiles();
for (File file : files) {
if (!file.canRead()) {
file.setReadable(true);
}
String content = “”;
try(BufferedReader br = new BufferedReader(new FileReader(file));) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
logger.info(“File ::::: ” + file.getName());
logger.info(” ——————————————————————————————————————————————————————— “);
while ((line = br.readLine()) != null)
{

if (line.contains(searchString) && (!line.contains(“System.out.println”) && !line.contains(“logger.info”) && !line.contains(“logger.debug”) && !line.contains(“logger.error”) && !line.contains(“//”) && !line.contains(“/*”)))
{
logger.info(“Matched Line :::::”+line);
}}
} finally {
br.close();
}
logger.info(” ##############################################################################################################################################################################################”);
}
} else {
System.out.println(“Not a Directory!”);
}
System.out.println(“DONE”);
}
public static void main(String args[]) throws IOException{

if(args.length == 0)
{
System.out.println(” ::::::::::::::::::::::::::::::::::::::::::”);
System.out.println(” :::::::::: PATH cannot be empty ::::::::::”);
System.out.println(” ::::::::::::::::::::::::::::::::::::::::::”);

logger.info(” ::::::::::::::::::::::::::::::::::::::::::”);
logger.info(” :::::::::: PATH cannot be empty ::::::::::”);
logger.info(” ::::::::::::::::::::::::::::::::::::::::::”);
System.exit(0);
}
searchFiles(new File(args[0]).getAbsolutePath(),”\””);}}

 

4)  ScanFilesLogger.java

package com.subhasish

import org.apache.log4j.Logger;
import org.apache.log4j.Logger;

public class ScanFilesLogger {

public static Logger getLogger(Class className) {
return Logger.getLogger(className);
}
public static Logger getLogger(String name) {

return Logger.getLogger(name);
}
}

 

5) Run Maven Clean and Install.It will create a jar file as SEARCHSTRING.jar

6) Create a Batch File – SEARCHSTRING.bat

@echo off
set /p input=”Enter Path::”
echo “Please Note : Your logs file will be genrated in C drive ”
echo “#########################################################”
java -jar SEARCHSTRING.jar %input%
pause

7) Place the jar and bat file at one place and run the bat file.

 

 

Advertisements

One thought on “Find/Search Hard Coded string in Files under Directories and Sub-Directories

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s