2010-09-17 142 views
2

我想在Windows XP中使用dos批处理文件编程在已存在的文本文件中的特定行写入一行文本。我也想从用户输入行号。任何帮助将不胜感激。windows批处理文件编程

回答

4

实例提示用户:

: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 

注:对于使用-L选项可以产生较大的交叉检查命令; 欲了解更多信息,请键入c:> FOR /?

FOR IN/L%变量(开始,步骤,结束)DO命令[命令参数]

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 %%一个IN(1,1,1000)DO如果“%TYPE %“==” %%一个”转到: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 
3

也许你不应该使用批处理。或者,也许你不应该使用批处理。

也许这样的事情可能会起作用。我没有经过测试。

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 

但是,如果您使用的是DOS而不是Windows NT的版本,它将无法正常工作。 (如果你使用的是Windows,而不是DOS编辑标记)

+1

感谢您的回复。我正在使用Windows XP。我打算为此写一个批处理文件。我尝试过这个。但是“!”符号会导致错误。我想在已经存在的文本文件的特定行中写入一行文本。同时我需要输入行号。来自user.Now此代码清除我现有的文件。更多帮助? – 1355 2010-09-17 10:47:41

+2

第六行应该是,如果不是“!counter!”==“%1”echo %% a >> newfile – 2010-09-17 15:06:53

+1

是的,我很久没有触及批处理。修正了。可以自由编辑我的帖子,为其他错误。 – BatchyX 2010-09-17 17:00:49