search for a string in files in folder


The Python script is belows

import re
import os
import sys
import time

#################################################################################
def listFileswithExtension(directory, extension):
“””Traverse in the directory and list all files of specific extension”””
list = os.listdir(directory)
fileList = []
for root, dirs, files in os.walk(directory, topdown=False):
for name in files:
# print “Searching file ” + name
(shortname, Extns) = os.path.splitext(name)
#print “Searching file ” + name + ” ” + Extns + ” ” + extension
if Extns == extension:
# print “Adding  ” & os.path.join(root, name)
fileList.append(os.path.join(root, name))
return fileList

#################################################################################
def replaceText(fileName, old, New, LogFileName):
“””Replaces the text in the text file”””
f = open(fileName, “r”)
fileString = f.read()
f.close()

processedString = re.sub(r'(?i)’ + old, New, fileString )
# pString = processedString
if fileString == processedString:
pass
return 0
else:
print(”  ** Updating the File: ” + fileName)
# Code to search and update the text on the file
f = open(fileName, “w”)
appendLogFile (”  ** Updating the File: ” + fileName + ‘\n’, LogFileName)

f.write(processedString)
f.close()
return 1

def appendLogFile(log,logFileName):
fLog = open(LogFileName,’a’)
fLog.writelines(log)
fLog.close()

# check the command line parameters
if len(sys.argv) == 6:
updateFileCount = 0
dirName = sys.argv[1]
TxtToRep = sys.argv[2]
txtNew = sys.argv[3]
extension = sys.argv[4]
LogFileName = sys.argv[5]

fileList = listFileswithExtension(dirName , extension)
print len(fileList)

strLog =  time.strftime(“Starting the scan at Date %Y-%m-%d, Time %H:%M:%S”)
appendLogFile(strLog + ‘\n’,appendLogFile)

strLog = “Searching for \n\tSearch Directory : ” + dirName + “\n”
strLog = strLog + “\tFile Extention   : ” + extension + “\n”
strLog = strLog + “\ttext to Search   : ” + TxtToRep + “\n”
strLog = strLog + “\ttext to Replace  : ” + txtNew + “\n”
appendLogFile(strLog, appendLogFile)

for fileNames in fileList:
print “Searching for update in file ” + fileNames
updateFileCount = updateFileCount + replaceText(fileNames,TxtToRep,txtNew, LogFileName)
strLog = “Updated ” + str(updateFileCount) + ” Files out of ” + str(len(fileList)) + “\nEnding Search…….\n”
print(strLog)
appendLogFile(strLog, appendLogFile)
else:
print(“Search and Replace Text. Version 0.4”)
print(“(C) Mayank Johri, johri.maya@gmail.com“)
print(“License: GNU 3”)
print(“=======================================”)
print(“”)
print(“Usage Error: – not all arguments were present”)
print(“Usage: search.exe <dirToSearch> <txtToRep> <txtNew> <FileExtension> <LogFile>”)
print(“Example: search.exe c:\\working test production .osd C:\\temp\\test.log” )

Advertisements

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