Link to home
Start Free TrialLog in
Avatar of purpleoak
purpleoakFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Search and replace with Batch

I have a file which contains lines in the following format

"93"      "Mr"      "Expolink Euorpe Ltd"      "Expolink Europe Ltd"      "Unit 1 Greenways Business Park"      "Bellinger Close"      "Chippenham"      "Wiltshire"      "SN15 1BN"      "U"      "27FEB06"

what i want to do is do a batch for loop which compairs the first token 93, with another file that is in the same format. I currently open the files with notepad ++ and do a find and replace for the string "     " with : then run the following script

for /F "tokens=1 delims=:" %%a in (osc.txt) do (for /F "tokens=1 delims=:" %%t in (ms520test.txt) do if %%a EQU %%t echo %%a:%%t >>results.txt)

This gives me what i want. However the find and replace normally crashes the program as the file has around 200K lines in it sometimes more. I need to be able to do a find and replace batch script or a for loop where the delims can be delims="      "" i have tried to escape the speech marks with the following delims=^"      ^"" but that didnt work

Any Ideas?
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

I seem to be finding the questions that I now think vbscript would be better for lately - this being one of them.  Can you clarify what you are looking to replace?  Also, you might consider using Excel if the data if formatted as such.  Is the number of spaces always the same?  If so, then I would suggest something like the code below (save the code as a .vbs file and make sure you change the content of the ReadFromFile and SaveToFile so they accurately reflect the file you want to open and the new one you want to save.


Option Explicit
 
'Declare the File name variables
Dim ReadFromFile, SaveToFile
 
'Declare the file system object variables
Dim objFSO, objReadFile, ObjCreateFile, objWriteFile
 
'Declare the source data variable
Dim SourceData
 
'Set the name and path of the file we are reading from
ReadFromFile="C:\temp\scripts\replacer.txt"
 
'Set the name and path to the file we are writing to (Creating)
SaveToFile="C:\temp\scripts\replaced.txt"
 
'Create and/or open the files to work with
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile(ReadFromFile)
Set objCreateFile = objFSO.CreateTextFile(SaveToFile)
ObjCreateFile.Close
Set objWriteFile = objFSO.OpenTextFile(SaveToFile, 2, True)
 
'Read every line of the file and append a comma to the end of every line.  
Do Until objReadFile.AtEndOfStream
 
        'Put the current line into the SourceData variable and remove any leading and trailing spaces,
		'Then replace the quotes-spaces-quotes portions.
        SourceData = Replace(Replace(Trim(objReadFile.ReadLine), chr(34) & string(6, " ") & chr(34), ":"), chr(34), "")
 
        'Write the modified source data 
        objWriteFile.WriteLine (SourceData)
Loop
 
'Close the Files
objReadFile.Close
objWriteFile.Close
Set objFSO = Nothing

Open in new window

Avatar of purpleoak

ASKER

Hi Leew

thanks for the quick responce, what im looking to find is "      " the find includes the speech marks and the whitespace is a tab. hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks guys for all your help, i had to do a search and replace with t0T0's script then did the for loop as described by oBdA. 1.5 million records took a LONG LONG time to process but it did the job. thank you for your help
Thanks for the quick response very very helpful
Great to be of assistance. Thank you for the feedback.