2

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.

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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)

5 Comments

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?
Sixth line should be if NOT "!counter!"=="%1" echo %%a >> newfile
Yes, i haven't touched batch for a long time. Fixed that. fell free to edit my post for other errors.
set /a counter+=1 When 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...
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??
2

User advanced batch file editor Dr.Batcher, you'll be able to create such scripts just in a few clicks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.