DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

How To Change A Character In All Documents

01.26.2011
| 1853 views |
  • submit to reddit
        how to change a character in all documents


Dir.chdir("D:\\change_characters")        #changes the current working directory
getFiles=`dir /B *.TXT`                   #passes this command to the shell
fileList=getFiles.split(/\n/)             #creates an Array separated with \n
fileList.length.times{                    #opening of a block
	fileName=fileList.pop             #pops the first item out of the Array
#############################
	fileA=File.open(fileName,"r")    #opens the file thats in fileName as read only
	doc=fileA.read                   #reads the file into the doc string
	fileA.close                      #closes the file
#############################
	doc.gsub!(/a/,"@")               #replaces "a" with "@" in the doc string
#############################
	fileB=File.open(fileName,"w")    #deletes and opens a the file in fileName
	fileB.write(doc)                 #writes the contents of doc into the file
	fileB.close                      #closes the file
#############################
}                                         #closing block