save to each line of a .txt file from .bat

Intel / Pars performance1100e
January 24, 2010 at 14:28:23
Specs: Windows XP, 1.12GB RAM
Hello, i am currently making something from a
batch file and i have it ALL worked out the
ONLY thing i cant do is save to each line. i
can get data and echo it from each line:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (file.txt) do (
set /a N+=1
set v!N!=%%a
)
set password=!v1!
set file=!v2!
echo %file%
echo %password%
pause


This basically gets line one and echo's it then
line two and echo's it but i dont realy want it to
echo i want it to save something to that line
eg:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (file.txt) do (
set /a N+=1
set v!N!=%%a
)
set password=!v1!
set file=!v2!
echo %file%
echo %password%
pause
cls
echo test1 > file.txt %file%>&1
pause


so that it will echo it then overwrite line one
with test1 in the. but the part:
echo test1 > file.txt %file%>&1
coesnt work please could someone make it
work (only that line).

Thank you


See More: save to each line of a .txt file from .bat

Report •


#1
January 24, 2010 at 16:12:12
echo test1 > file.txt %file%>&1
won't work the way you want, i believe.
batchscript can't "replace" lines of text directly, that i know of. it has to copy the file to another file then rename or copy the new file back to the original file:
for /f "tokens=* delims= " %%a in (file.txt) do (
set /a N+=1
if !N! equ 1 (>>temp.txt echo test1) else (>>temp.txt echo %%a)
set v!N!=%%a
)
then at end of your script:
move /y temp.txt file.txt
edlin will replace a line directly, but you have to create a control-file ("pipe file") to feed into edlin:
1
this is new text
e

to replace line one.
@echo off & setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%a in (file.txt) do (
set /a N+=1
set v!N!=%%a
)
set password=!v1!
set file=!v2!
echo %file%
echo %password%
pause
cls
>ed echo 1
>>ed echo test1
>>ed echo e
edlin file.txt<ed
del ed

either way you wind up having to create a second file since (unfortunately) echo can't echo the newline (0D0A)


Report •
Related Solutions


Ask Question