I want to write a line of text at a specific line in an already existing text file using dos batch file programming in Windows XP.Also I want to input the line number from the user. Any help will be appreciated.
+2
A:
Maybe you shouldn't be using Batch for this. Or maybe you shouldn't be using batch at all.
Maybe something like this may work. i've not tested it throught.
setlocal enabledelayedexpansion
SET /a counter=0
echo. > newfile
for /f "usebackq delims=" %%a in (yourfile.txt) do (
if "!counter!"=="%1" echo "YOUR SPECIFIC LINE" >> newfile
if not "!counter!"=="%1" echo %%a >> newfile
set /a counter+=1
)
move newfile yourfile.txt
But it won't work if you are using DOS and not a version of Windows NT. (edit your tags if you are using windows and not dos)
BatchyX
2010-09-17 08:23:06
Thanks for the earlier reply.I am using Windows XP. I intend to write a batch file for this. I tried it. But "!" symbol causes error.I want to write a line of text at a specific line in an already existing text file.also i need to input the line no. from user.Now this code clears my existing file. More help?
Sarika
2010-09-17 10:47:41
Sixth line should be if NOT "!counter!"=="%1" echo %%a >> newfile
Andy Morris
2010-09-17 15:06:53
Yes, i haven't touched batch for a long time. Fixed that. fell free to edit my post for other errors.
BatchyX
2010-09-17 17:00:49
set /a counter+=1When i checked the value of counter variable it is seen that the above statement is not at all working.The value is not changing in the loop. Thanks in advance...
Sarika
2010-09-20 06:43:44
Is more help????????
Sarika
2010-09-20 09:33:45
How can i replace only an existing line of text with a new line of text in a text file. Is it possible to retain the old line of text also.Is anyone to help me??
Sarika
2010-09-20 09:52:34
comments closed
Sarika
2010-09-22 05:23:10
A:
Example of prompting the user:
:MENU
SET /P TYPE=Type the line number and press enter:
if "%TYPE%"=="1" goto ONE
if "%TYPE%"=="2" goto TWO
if "%TYPE%"=="3" goto THREE
if "%TYPE%"=="4" goto FOUR
if "%TYPE%"=="5" goto FIVE
goto MENU
Note: The FOR command with the L option can generate a greater crosscheck; for more info type c:>FOR /?
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)
FOR /L %%a IN (1,1,1000) DO if "%TYPE%"=="%%a" goto :VALIDNUM
@echo off
rem this only prompts the user for a number
set VALIDNUM=
:MENU
cls
echo.
echo.
If NOT "%VALIDNUM%"=="" echo the number is %VALIDNUM%
echo.
SET /P TYPE=Type a line number and press enter:
FOR /L %%a IN (1,1,1000) DO if "%TYPE%"=="%%a" set VALIDNUM=%TYPE%
goto MENU
Edoctoor
2010-09-17 19:07:21