Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need search/replace script

49 views
Skip to first unread message

M.L.

unread,
Jul 4, 2012, 6:39:38 AM7/4/12
to

I need a CMD "find and replace" script for Vista/Win7 that meets the
following conditions:

1.) No 3rd party downloads allowed. Just CMD and Notepad.

2.) Scans multiple text files with a given extension (or extensions)
within a given directory. Subdirectory scanning is a plus but not
necessary.

3.) The search/replace inputs can be set within the script or within a
separate text file (within the script preferred).

4.) Each search/replace input should be allowed to contain multiple
words.

5.) The script should be able to perform for multiple search/replace
given pairs. Multiline search/replace is a plus but not necessary.

6.) The script should set the modified file to the original name, but
copy the original file to a *.bak extension. The coding to save the
backup should be able to be commented out to make the backup
processing optional.

I've looked for some relevant scripts but they all seem to be missing
one or more of my conditions, and I'm not good at CMD scripting, so
any help will be appreciated. Thanks.

foxidrive

unread,
Jul 4, 2012, 9:26:24 AM7/4/12
to
On Wednesday 04/07/2012 20:39, M.L. wrote:
>
> I need a CMD "find and replace" script for Vista/Win7 that meets the
> following conditions:
>
> 1.) No 3rd party downloads allowed. Just CMD and Notepad.
>
> 2.) Scans multiple text files with a given extension (or extensions)
> within a given directory. Subdirectory scanning is a plus but not
> necessary.
>
> 3.) The search/replace inputs can be set within the script or within a
> separate text file (within the script preferred).
>
> 4.) Each search/replace input should be allowed to contain multiple
> words.

Give an example please.

I have a SAR script using WSH/VBS which is default in the OS from XP onward and that meets your requirements - will need to add the folder/file scanning but I'd like to see the format of your search and replace terms.


> 5.) The script should be able to perform for multiple search/replace
> given pairs. Multiline search/replace is a plus but not necessary.
>
> 6.) The script should set the modified file to the original name, but
> copy the original file to a *.bak extension. The coding to save the
> backup should be able to be commented out to make the backup
> processing optional.
>
> I've looked for some relevant scripts but they all seem to be missing
> one or more of my conditions, and I'm not good at CMD scripting, so
> any help will be appreciated. Thanks.
>


--
Mic


Todd Vargo

unread,
Jul 5, 2012, 1:57:59 PM7/5/12
to
This appears to be a word-for-word homework assignment. Are you looking
for help or someone to spoon feed you with ready to turn in code? Show
us what relevant code you have tried, explain how you have tried to
combine them, where you are stuck with the code, etc. If you are not
willing to show some effort on your part, then why would anyone offer
their time to what appears to be a homework assignment?

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)


Dr J R Stockton

unread,
Jul 5, 2012, 1:15:40 PM7/5/12
to
In alt.msdos.batch message <NwXIr.62840$GJ4....@newsfe16.iad>, Wed, 4
Jul 2012 23:26:24, foxidrive <foxi...@gotcha.woohoo.invalid> posted:

>
>I have a SAR script using WSH/VBS which is default in the OS from XP onward and that meets your requirements - will need to add the folder/file
>scanning but I'd like to see the format of your search and replace terms.
>

Personally, I'd prefer WSH/JS. But in either case, I'd recommend
reading <http://wsh2.uw.hu/index.html> ff.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

foxidrive

unread,
Jul 5, 2012, 8:37:51 PM7/5/12
to
On Friday 06/07/2012 03:15, Dr J R Stockton wrote:
> In alt.msdos.batch message <NwXIr.62840$GJ4....@newsfe16.iad>, Wed, 4
> Jul 2012 23:26:24, foxidrive <foxi...@gotcha.woohoo.invalid> posted:
>
>>
>> I have a SAR script using WSH/VBS which is default in the OS from XP onward and that meets your requirements - will need to add the folder/file
>> scanning but I'd like to see the format of your search and replace terms.
>>
>
> Personally, I'd prefer WSH/JS.

Hi John, I already have VBS code from this group that does the job - what is superior about Jscript for this task?

> But in either case, I'd recommend
> reading <http://wsh2.uw.hu/index.html> ff.

A book? I'm sure it has good stuff in it, but what are you alluding to?



--
Mic


M.L.

unread,
Jul 6, 2012, 10:20:24 PM7/6/12
to


>> I need a CMD "find and replace" script for Vista/Win7 that meets the
>> following conditions:
>>
>> 1.) No 3rd party downloads allowed. Just CMD and Notepad.
>>
>> 2.) Scans multiple text files with a given extension (or extensions)
>> within a given directory. Subdirectory scanning is a plus but not
>> necessary.
>>
>> 3.) The search/replace inputs can be set within the script or within a
>> separate text file (within the script preferred).
>>
>> 4.) Each search/replace input should be allowed to contain multiple
>> words.
>
>Give an example please.

I'd just like a CMD script to do part of what find/replace programs
such as Textcrawler can do, which is replace a word or phrase inside a
file with another word or phrase. I don't know if I can deconstruct my
needs further.

>I have a SAR script using WSH/VBS which is default in the OS from XP onward and that meets your requirements - will need to add the folder/file scanning but I'd like to see the format of your search and replace terms.

I'd rather not use WSH/VBS since I'm not familiar with it and won't be
able to tweak it if needed.

foxidrive

unread,
Jul 6, 2012, 11:08:01 PM7/6/12
to
On Saturday 07/07/2012 12:20, M.L. wrote:
>
>
>>> 4.) Each search/replace input should be allowed to contain multiple
>>> words.

>> Give an example please.
>
> I'd just like a CMD script to do part of what find/replace programs
> such as Textcrawler can do, which is replace a word or phrase inside a
> file with another word or phrase. I don't know if I can deconstruct my
> needs further.

I don't use textcrawler. What I want to know is are you replacing "orange" with "apple" and multiple times on every line, and "get off my lawn" with "The water is fine".
Is there any other format of search and replace that you need?


>> I have a SAR script using WSH/VBS which is default in the OS from XP onward and that meets your requirements - will need to add the folder/file scanning but I'd like to see the format of your search and replace terms.
>
> I'd rather not use WSH/VBS since I'm not familiar with it and won't be
> able to tweak it if needed.

Apparently you can't write a batch file either, so now you are being choosey? :)


--
Mic


M.L.

unread,
Jul 7, 2012, 4:42:34 AM7/7/12
to


>>>> 4.) Each search/replace input should be allowed to contain multiple
>>>> words.
>
>>> Give an example please.
>>
>> I'd just like a CMD script to do part of what find/replace programs
>> such as Textcrawler can do, which is replace a word or phrase inside a
>> file with another word or phrase. I don't know if I can deconstruct my
>> needs further.
>
>I don't use textcrawler. What I want to know is are you replacing "orange" with "apple" and multiple times on every line, and "get off my lawn" with "The water is fine".

Yes, that's what I'm looking for. Multiple times on each line, if
needed, throughout the file. I'd prefer the search/replace pairs to be
set within the script so it can easily be modified for reuse as an
all-in-one script.

Thanks for your reply.

foxidrive

unread,
Jul 7, 2012, 5:05:47 AM7/7/12
to
On Wednesday 04/07/2012 20:39, M.L. wrote:
>
Are you still against WSH/VBS?



--
Mic


Todd Vargo

unread,
Jul 7, 2012, 7:45:35 AM7/7/12
to
Or more informatively...

How about replacing "apples and oranges" with "peaches & pears"?
Or "Hello." with "Good morning!"

...note the punctuation changes.

A batch file is a major step backwards from what TextCrawler offers. Of
course, any good word processor offers SAR plus spelling/grammatical
correction. Why reinvent the wheel?

Dr J R Stockton

unread,
Jul 7, 2012, 2:20:47 PM7/7/12
to
In alt.msdos.batch message <%rqJr.16284$%Y2....@newsfe02.iad>, Fri, 6
Jul 2012 10:37:51, foxidrive <foxi...@gotcha.woohoo.invalid> posted:

>On Friday 06/07/2012 03:15, Dr J R Stockton wrote:
>> In alt.msdos.batch message <NwXIr.62840$GJ4....@newsfe16.iad>, Wed, 4
>> Jul 2012 23:26:24, foxidrive <foxi...@gotcha.woohoo.invalid> posted:
>>
>>>
>>> I have a SAR script using WSH/VBS which is default in the OS from XP
>>>onward and that meets your requirements - will need to add the
>>>folder/file
>>> scanning but I'd like to see the format of your search and replace terms.
>>>
>>
>> Personally, I'd prefer WSH/JS.
>
>Hi John, I already have VBS code from this group that does the job -
>what is superior about Jscript for this task?

I know JavaScript better than I do VBScript. JavaScript is more
generally useful, since all common Web browsers know JavaScript, but
only MS IE knows VBScript. Under WSH, JS & VBS APIs are equally
capable, though JS users commonly have to read VBS documentation.


>> But in either case, I'd recommend
>> reading <http://wsh2.uw.hu/index.html> ff.
>
>A book? I'm sure it has good stuff in it, but what are you alluding to?

Yes and no. Follow the URL to find out. THAT page is under 12 kB of
simple HTML text.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike 6.05 WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

foxidrive

unread,
Jul 7, 2012, 9:48:54 PM7/7/12
to
On Sunday 08/07/2012 04:20, Dr J R Stockton wrote:

>>> Personally, I'd prefer WSH/JS.
>>
>> Hi John, I already have VBS code from this group that does the job -
>> what is superior about Jscript for this task?
>
> I know JavaScript better than I do VBScript. JavaScript is more
> generally useful, since all common Web browsers know JavaScript, but
> only MS IE knows VBScript. Under WSH, JS & VBS APIs are equally
> capable, though JS users commonly have to read VBS documentation.

Ahh, personal preference. Gotcha. I thought you meant there was a technical reason.

>
>>> But in either case, I'd recommend
>>> reading <http://wsh2.uw.hu/index.html> ff.
>>
>> A book? I'm sure it has good stuff in it, but what are you alluding to?
>
> Yes and no. Follow the URL to find out. THAT page is under 12 kB of
> simple HTML text.

I went to the URL before you got the reply. It's a book.

It seems that you're recommending it for general purpose use and has nothing specifically to do with the question. Gotcha.


--
Mic


Dr J R Stockton

unread,
Jul 8, 2012, 2:03:13 PM7/8/12
to
In alt.msdos.batch message <gp6fv7pk5ev0rs7au...@4ax.com>,
Fri, 6 Jul 2012 21:20:24, M. L. <m...@privacy.invalid> posted:

>I'd rather not use WSH/VBS since I'm not familiar with it and won't be
>able to tweak it if needed.

You need to be able (A) to read an input file, and a file or command-
line of substitutions, (B) construct and apply the necessary RegExps,
(C) write an output file.

(A) & (C) are explained at the URL I gave before; (B) is straightforward
programming in VBscript or JavaScript, and you can ask in the
appropriate newsgroups for those.

My 32-bit seakfyle.js (IIRC, get ZIP via
<http://www.merlyn.demon.co.uk/programs/32-bit/00index.htm>) does (A)
(B) and (C), though differently arranged; but it contains a lot of other
stuff too.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links.

Dr J R Stockton

unread,
Jul 9, 2012, 2:43:04 PM7/9/12
to
In alt.msdos.batch message <XG5Kr.42736$FV6....@newsfe05.iad>, Sun, 8
Jul 2012 11:48:54, foxidrive <foxi...@gotcha.woohoo.invalid> posted:

>On Sunday 08/07/2012 04:20, Dr J R Stockton wrote:
>
>>>> Personally, I'd prefer WSH/JS.
>>>
>>> Hi John, I already have VBS code from this group that does the job -
>>> what is superior about Jscript for this task?
>>
>> I know JavaScript better than I do VBScript. JavaScript is more
>> generally useful, since all common Web browsers know JavaScript, but
>> only MS IE knows VBScript. Under WSH, JS & VBS APIs are equally
>> capable, though JS users commonly have to read VBS documentation.
>
>Ahh, personal preference. Gotcha. I thought you meant there was a
>technical reason.

Well, it is a better language, more generally useful and not reliant on
the whim of a single supplier.


>>>> But in either case, I'd recommend
>>>> reading <http://wsh2.uw.hu/index.html> ff.
>>>
>>> A book? I'm sure it has good stuff in it, but what are you alluding to?
>>
>> Yes and no. Follow the URL to find out. THAT page is under 12 kB of
>> simple HTML text.
>
>I went to the URL before you got the reply. It's a book.

It is the first (web) page of a complete book or large booklet. Perhaps
you did not understand the "ff".


>It seems that you're recommending it for general purpose use and has
>nothing specifically to do with the question. Gotcha.

No, it contains all that the OP should need for I/O, in VBS & in JS. It
contains only little about RegExps (other sites have more); but
<http://wsh2.uw.hu/ch12e.html> has a final section "Replacing Text in a
File" - the OP will only need to make it do multiple variable
replacements instead of a single fixed one. The JavaScript part of my
site might help there. But it is also good for general purposes.

Caveat : the site was perhaps written in MSIE. In my MSIE, Opera,
Safari, Chrome, spaces in examples look normal. In my Firefox 13.0.1,
they show as black diamonds containing a white question mark. They seem
to have been sent as ASCII 160, the "fixed space". All in WinXP sp3.


Perhaps you should have paid more attention to the contents of the site
before commenting on it. If you now feel like Richard III, there's an
URL on my site that might help one of your TZ.

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

foxidrive

unread,
Jul 9, 2012, 7:34:52 PM7/9/12
to
On Tuesday 10/07/2012 04:43, Dr J R Stockton wrote:

>>>>> But in either case, I'd recommend
>>>>> reading <http://wsh2.uw.hu/index.html> ff.
>>>>
> It is the first (web) page of a complete book or large booklet. Perhaps
> you did not understand the "ff".

Firefox, obviously.

> Perhaps you should have paid more attention to the contents of the site
> before commenting on it.

John, you're the person that included it in your conversation without commenting on it. :) I went to the trouble of visiting the web page and it had nothing specifically to do with the thread.

And ff?? Here, pick one. http://www.google.com.au/search?hl=en&q=ff&btnG=Google+Search




--
Mic


Dr J R Stockton

unread,
Jul 11, 2012, 2:23:01 PM7/11/12
to
In alt.msdos.batch message <2VJKr.22407$cE7....@newsfe13.iad>, Tue, 10
Jul 2012 09:34:52, foxidrive <foxi...@gotcha.woohoo.invalid> posted:
Google finds anything it can; Wikipedia is more selective, and has a
page for "ff.", which should not be confused with the page for "Ff.".

--

foxidrive

unread,
Jul 11, 2012, 7:10:56 PM7/11/12
to
On Thursday 12/07/2012 04:23, Dr J R Stockton wrote:

Here John, read that part again.

>> John, you're the person that included it in your conversation without
>> commenting on it. :) I went to the trouble of visiting the web page
>> and it had nothing specifically to do with the thread.


--
Mic


M.L.

unread,
Jul 12, 2012, 9:52:17 PM7/12/12
to
I'd prefer not to use WSH/VBS because it might be locked from use on
some computers. I'd rather stick with CMD scripting. I'm disappointed
that the thread has veered to Javascript usage since I don't think
it's capable of saving results to a file.

Todd Vargo

unread,
Jul 12, 2012, 10:53:45 PM7/12/12
to
On 7/12/2012 9:52 PM, M.L. wrote:
>
>> Are you still against WSH/VBS?
>
> I'd prefer not to use WSH/VBS because it might be locked from use on
> some computers. I'd rather stick with CMD scripting. I'm disappointed
> that the thread has veered to Javascript usage since I don't think
> it's capable of saving results to a file.

Better to stick with asking questions about what you don't know about
than throwing incorrect assumptions around. Of course js can write to a
file the same way vbs can.

Dr J R Stockton

unread,
Jul 13, 2012, 2:36:15 PM7/13/12
to
In alt.msdos.batch message <qKnLr.31894$7y4....@newsfe23.iad>, Thu, 12
Jul 2012 09:10:56, foxidrive <foxi...@gotcha.woohoo.invalid> posted:
The actual page first cited was <http://wsh2.uw.hu/index.html>. It is
an index page. Other pages can be reached by clicking on the links to
chapters and their sections. The entire document is of use to those who
want to use WSH or HTA. Page <http://wsh2.uw.hu/ch12e.html> has a final
section "Replacing Text in a File" - the OP should only need to make it
do multiple variable replacements instead of a single fixed one.

Never mind : even if you so not understand, The OP may find it useful.

Dr J R Stockton

unread,
Jul 14, 2012, 1:35:31 PM7/14/12
to
In alt.msdos.batch message <ngvuv7t1dmifrujck...@4ax.com>,
Thu, 12 Jul 2012 20:52:17, M. L. <m...@privacy.invalid> posted:
Then you should read <http://wsh2.uw.hu/index.html> etc.

Strictly, JavaScript, JScript, and VBscript can neither read not write.
But they can call a Windows API routine, when in a suitable environment
(not a Web page; but under WSH or in an HTA).

My 32-bit seakfyle.js (IIRC, get ZIP via
<http://www.merlyn.demon.co.uk/programs/32-bit/00index.htm>) has code
for subdirectory scanning; a novice programmer would be well advised to
copy something like that rather than to attempt it unaided.

--

cel...@wideopenwest.com

unread,
Jul 15, 2012, 6:09:55 PM7/15/12
to
On Thursday, July 5, 2012 1:57:59 PM UTC-4, Todd Vargo wrote:
> On 7/4/2012 6:39 AM, M.L. wrote:
> &gt;
> &gt; I need a CMD &quot;find and replace&quot; script for Vista/Win7 that meets the
> &gt; following conditions:
> &gt;
> &gt; 1.) No 3rd party downloads allowed. Just CMD and Notepad.
> &gt;
> &gt; 2.) Scans multiple text files with a given extension (or extensions)
> &gt; within a given directory. Subdirectory scanning is a plus but not
> &gt; necessary.
> &gt;
> &gt; 3.) The search/replace inputs can be set within the script or within a
> &gt; separate text file (within the script preferred).
> &gt;
> &gt; 4.) Each search/replace input should be allowed to contain multiple
> &gt; words.
> &gt;
> &gt; 5.) The script should be able to perform for multiple search/replace
> &gt; given pairs. Multiline search/replace is a plus but not necessary.
> &gt;
> &gt; 6.) The script should set the modified file to the original name, but
> &gt; copy the original file to a *.bak extension. The coding to save the
> &gt; backup should be able to be commented out to make the backup
> &gt; processing optional.
> &gt;
> &gt; I&#39;ve looked for some relevant scripts but they all seem to be missing
> &gt; one or more of my conditions, and I&#39;m not good at CMD scripting, so
> &gt; any help will be appreciated. Thanks.
>
> This appears to be a word-for-word homework assignment. Are you looking
> for help or someone to spoon feed you with ready to turn in code? Show
> us what relevant code you have tried, explain how you have tried to
> combine them, where you are stuck with the code, etc. If you are not
> willing to show some effort on your part, then why would anyone offer
> their time to what appears to be a homework assignment?
>
> --
> Todd Vargo
> (Post questions to group only. Remove &quot;z&quot; to email personal messages)

Who the hell do you think you are? I have never seen a forum where a person places these types of stipulations on someone asking a question. What difference does it make to you what reason this person is doing this for. Help don't help, that is up to you. But, don't set here and demand shit of people. When someone asks me for help, I help them. If I need to see their code to help that is different, but otherwise I could less what effort they have put into it. That is not a reason to help people.

Todd Vargo

unread,
Jul 16, 2012, 4:55:56 PM7/16/12
to
> Who the hell do you think you are? I have never seen a forum where a person places these types of stipulations on someone asking a question. What difference does it make to you what reason this person is doing this for. Help don't help, that is up to you. But, don't set here and demand shit of people. When someone asks me for help, I help them. If I need to see their code to help that is different, but otherwise I could less what effort they have put into it. That is not a reason to help people.
>

I don't see your solution. The OP posted this eleven days ago, but,
instead of cobbling together your own solution to help the OP, all you
do is complain that I did not offer my time for one. I have seen your
kind of help (palpay). If you are willing to pay me on behalf of the OP,
I would be willing to post some code for this particular request.
Otherwise, post your own code solutions and don't worry about anyone else.

--
Todd Vargo
0 new messages