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.
Copy text file1-search file2-replace search with copied
Moderator: DosItHelp
Re: Copy text file1-search file2-replace search with copied
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%"
Re: Copy text file1-search file2-replace search with copied
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!!!!
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!!!!
Re: Copy text file1-search file2-replace search with copied
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%"