开发者

windows batch file programming

开发者 https://www.devze.com 2023-01-16 07:32 出处:网络
I want to write a line of text at a specific line in an a开发者_开发技巧lready existing text file using dos batch file programming in Windows XP.Also I want to input the line number from the user. Any

I want to write a line of text at a specific line in an a开发者_开发技巧lready 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.


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


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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号