Copy text file1-search file2-replace search with copied

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
imasri
Posts: 2
Joined: 30 Aug 2012 14:40

Copy text file1-search file2-replace search with copied

#1 Post by imasri » 30 Aug 2012 15:47

Hello, I can't seem to figure out how to add a new post. I would like to ask the forum for help with a batch script I'm working on.
I basically have two text files, (find.txt) and (replace.txt). I have a script that performs an action that outputs a computer's serial number, or specifically a Dell Service Tag, and places that serial number in (find.txt). (replace.txt) has about 40 lines of data, and on line 35 it says NODENAME=<IGNORE>. What I want to do is to copy the Service Tag from (find.txt) and then search for "<IGNORE>" in (replace.txt) and replace "<IGNORE>" with the service tag. The (find.txt) file only has one line of data, and it is the service tag. I hope I explained correctly. Let me know how I can post this or if there is any help I can get. Thank you.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy text file1-search file2-replace search with copied

#2 Post by foxidrive » 30 Aug 2012 20:50

Try this:

Code: Select all

:: Search and replace
@echo off
set "oldstr=<IGNORE>"
set "file=replace.txt"
set "newfile=replace2.txt"

set /p "newstr="<find.txt >nul

set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")
type "%file%" |cscript /nologo "%tempfile%"  > "%newfile%"
del "%tempfile%"

imasri
Posts: 2
Joined: 30 Aug 2012 14:40

Re: Copy text file1-search file2-replace search with copied

#3 Post by imasri » 31 Aug 2012 07:27

Hello Foxidrive, man oh man, that worked like a charm!!!!! Thank you very much, i've been searching online all week and got ways to try to do it, but never as accurate as what I needed that you provided. Thank you, thank you, thank you.

One more thing, in addition to it replacing it with the Service Tag, I need to append the letter "R" with that, so for example:

Service Tag = 11QMZQ1 --> which is located in (find.txt)

I need it to paste it in (replace.txt) replacing "<IGNORE>" with "R+11QMZQ1" ----- so final replace would be R11QMZQ1

Any further help is greatly appreciated!!!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy text file1-search file2-replace search with copied

#4 Post by foxidrive » 31 Aug 2012 07:34

Try this, there is one extra line to add the R. I'm glad it helped you.

Code: Select all

:: Search and replace
@echo off
set "oldstr=<IGNORE>"
set "file=replace.txt"
set "newfile=replace2.txt"

set /p "newstr="<find.txt >nul

set "newstr=R%newstr%"

set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")
type "%file%" |cscript /nologo "%tempfile%"  > "%newfile%"
del "%tempfile%"

Post Reply