1.

Solve : how to find and replace word in txt file?

Answer»

i WOULD like to FIND a string like 'abc' and replace it with 'cde'...how do i do so in dos? Real DOS or some kind of Windows command prompt?actually looking in dos command Which version? Different versions are acting different.

uliYou could probably do this with set STATEMENTS. But why? In DOS use EDIT and do a find and replace operation (found on the search drop down menu).

I'm actually using Win XP. No doubt i can use edit but there is about 100 over file to change ...therefore looking into a dos command to change the file There is no DOS command to do a find and replace. There are a couple that can do the find, but not the replace. As mentioned earlier any editor can do this, or you can write a Windows Script (this is Windows after all). Are all the files in the same directory?

Let us know. yeap you are CORRECT...all the files is in the same directory...actually i not really care what script to used as long as it able to do so

ThanksCode: [Select]
Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("folderspec") ' use valid folder
Set fc = f.Files
For Each fs in fc
Set objFile = fso.OpenTextFile(fs, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Find", "Replace") ' case sensitive

Set objFile = fso.OpenTextFile(fs, ForWriting)
objFile.WriteLine strNewText
objFile.Close
Next


Save script with a vbs extension. RUN from command line: cscript scriptname.vbs

Note: Change folderspec to something valid (must be quoted). Change Find and Replace to your search argument and it's replacement (case sensitive and must be quoted)



Discussion

No Comment Found