0

i am writing batch script from which i need to update text file in specific line. for eg: In a text file line number 30 has say "080-22368865 Ware house", Script has to change only "080-22368865" not the ware house, as user enter in the command prompt after running the script as "022-26986528" Then the in the text file it should show "022-26986528 ware-house" in the line number 30.

Thanks in advance.

1
  • so, what have you tried? what problems you found? if you want jst a starting point, please, be more precise on your problem. Is that Windows? is always the same line number? how can you change with something the user enters after running the script? Commented Nov 11, 2012 at 9:40

1 Answer 1

2

Your question missed several details, so the Batch file below is just a starting point. The line number is given in the first parameter.

@echo off
setlocal EnableDelayedExpansion
call :editLine %1 < input.txt > output.txt
goto :EOF

:editLine num
set /A skipLines=%1-1
if %skipLines% gtr 0 (
   rem Copy lines before the target
   for /L %%i in (1,1,%skipLines%) do (
      set line=
      set /P line=
      echo(!line!
   )
)
rem Edit the target line
set line=
set /P line=
echo Line %1: "!line!"
for /F "tokens=1*" %%a in ("!line!") do (
   set /P firstToken=Enter new value for "%%a": 
   echo !firstToken! %%b
)
rem Copy the rest of lines
:nextLine
   set line=
   set /P line=
   if not defined line exit /B
   echo !line!
goto nextLine

Previous program fails if there is an empty line after the target line: the copy process stop at that point. As I said before, this and other details can be fixed...

Sign up to request clarification or add additional context in comments.

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.