Write output to specific place in text file?

Microsoft Microsoft windows xp pro full...
January 6, 2010 at 11:54:43
Specs: Windows XP, 2.4 3GB
Is there anyway to write to a specific place in a text file? For example, I am asking the user: set /p filepath=What is the unc path to the first .msp file?:
I'd like to have a text file with the text "MSP ONE" in it and write the result of this user input directly after "MSP ONE" in the text file.

I know I can use: echo %filepath% >> C:\mytest\outputtest.txt
and this will append the text to the bottom of the text file.. but I want to place it in the middle after MSP ONE.
Possible?
Thanks.


See More: Write output to specific place in text file?

Report •

#1
January 6, 2010 at 12:24:21
so, i'll just cheat and fixt this one while i'm at it:


@echo off & setlocal
set /p line=filename:
set target=msp one
for /f "tokens=* delims=" %%a in (file.txt) do (
>> newfile.txt echo %%a
if /i "%%a" equ "%target%" >>newfile.txt echo %line%
)
move /y newfile.txt file.txt


Report •

#2
January 6, 2010 at 12:38:39
worked like a charm.. thx.

Report •

#3
January 6, 2010 at 13:30:05
Did you edit this? I came back to copy.. and now it won't work. I stuck pauses in .. and it creates the newfile.txt and loops through, but it doesn't write to newfile.txt now.. and therefore doesn't copy. I'm utterly confused. When I first tried it, it worked.

Report •

Related Solutions

#4
January 6, 2010 at 14:20:17
odd.. i've found this only works the first time. If I run it again, it won't update the text. If I delete my text document and recreate it, it will work the first time through again.

Report •

#5
January 6, 2010 at 14:51:11
sorry about that. it was putting spaces at the ends of the lines which blew up all subsequent compare operations.
i did edit the code, but the code (as it stands now) should work, at least it did over here. check your outputfile after running and make sure it's not putting space at end of lines.

Report •

#6
January 7, 2010 at 07:26:21
It does work.. but only the first time through.. any subsequent runs "breaks" it.
How would I know if it's inserting spaces at the end?.. which I suspect you're right about. It's the only reason that makes sense why it would't run twice. It doesn't seem to find the delimiter when parsing the text file after the first run.
A better question for me might be "How would I stop it from adding spaces at the end?"

Report •

#7
January 7, 2010 at 10:08:41
y, sorry again! a SPACE hitched a ride on the end of line 5 of my code, which i did not see until i cut/pasted it.
use EDIT or NOTEPAD, got to line 5:
>> newfile.txt echo %%a
hit the END key and you will see the sneaky little b---tard sitting there, no doubt smirking at me. just use backspace, (then DEL and ENTER just for insurance) to remove him, and re-save the file.
highly embarassed about this, and again, apologies. it seems like the simplest operations sometimes give you the most fits.
(i will remove the space from my post as well, so if you want to start from scratch, it will be corrected)

Report •

Ask Question