CODE: [Copy to clipboard]
@echo off
setlocal enabledelayedexpansion
echo.
echo 文本编辑 [版本 1.0.0025]
echo Coded by neko, 2010. All rights reserved.
::临时文件设定
set tempfile=%temp%\edit.tmp
::控制入口设定
set ctrcmd=!control
::文件名设定
if not "%1"=="" (set filename=%1
goto EDITOLD)
goto CONTROL
:CONTROL
echo.
:CONTCYCLE
set /p ecmd=">"
for /f "tokens=1" %%i in ("%ecmd%") do set ecmdf=%%i
for /f "tokens=2" %%i in ("%ecmd%") do set ecmd1=%%i
::文件操作
if "%ecmdf%"=="new" (
set filename=新建文本文档.txt
echo.
goto CREATENEW
)
if "%ecmdf%"=="open" (
set filename=!ecmd1!
goto EDITOLD
)
if "%ecmdf%"=="save" (
if exist "!filename!.bak" del "!filename!.bak" /f
if exist "!filename!" move "!filename!" "!filename!.bak"
copy "%tempfile%" "!filename!"
goto EDITOLD
)
if "%ecmdf%"=="saveas" (
set filename=!ecmd1!
if exist "!filename!.bak" del "!filename!.bak" /f
if exist "!filename!" move "!filename!" "!filename!.bak"
copy "%tempfile%" "!filename!"
goto EDITOLD
)
if "%ecmdf%"=="back" (
if "%editstate%"=="CREATE_NEW" set /a line=!line!-1
echo.
goto EDITPARA
)
if "%ecmdf%"=="exit" (
if exist "%tempfile%" del "%tempfile%" /f
goto EOF
)
if "%ecmdf%"=="help" goto HELP
::行操作
if "%ecmdf%"=="insert" goto INSERT
if "%ecmdf%"=="line.insert" goto LINSERT
if "%ecmdf%"=="line.delete" goto LDELETE
if "%ecmdf%"=="line.edit" goto LEDIT
echo '%ecmdf%' 不是有效指令,请键入 help 查询控制命令。
echo.
goto CONTCYCLE
:NOPEN
echo 未打开任何可编辑的文件。
goto CONTROL
:NOLINE
echo 指定行数不存在。
goto CONTROL
:HELP
echo.
echo 新建 new
echo 打开 open [filename]
echo 保存 save
echo 另存为 saveas [filename]
echo 返回 back
echo 退出 exit
echo 帮助 help
echo.
echo 插入段 insert [行号]
echo 插入行 line.insert [行号]
echo 删除行 line.delete [行号]
echo 编辑行 line.edit [行号]
goto CONTROL
:INSERT
if "!filename!"=="" goto NOPEN
if !ecmd1! LEQ 0 goto NOLINE
set /a line=!line!+1
if !ecmd1! GTR !line! goto NOLINE
echo.
if exist "%tempfile%.1" del "%tempfile%.1" /f
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! LSS !ecmd1! echo %%i>>"%tempfile%.1"
)
set /a line=!ecmd1!-1
:INSPARA
set /a line=!line!+1
set /p cLine="%line%: "
if not "%cLine%"=="%ctrcmd%" (
echo ^%cLine%>>"%tempfile%.1"
goto INSPARA)
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! GEQ !ecmd1! echo %%i>>"%tempfile%.1"
)
if exist "%tempfile%" del "%tempfile%" /f
move "%tempfile%.1" "%tempfile%"
goto EDITCUR
:LINSERT
if "!filename!"=="" goto NOPEN
if !ecmd1! LEQ 0 goto NOLINE
if !ecmd1! GTR !line! goto NOLINE
echo.
set /p cLine="!ecmd1!: "
if exist "%tempfile%.1" del "%tempfile%.1" /f
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! LSS !ecmd1! echo %%i>>"%tempfile%.1"
if !line! EQU !ecmd1! echo ^%cLine%>>"%tempfile%.1"
)
set /a ecmd1=!ecmd1!-1
for /f "skip=%ecmd1% tokens=*" %%i in (%tempfile%) do echo %%i>>"%tempfile%.1"
if exist "%tempfile%" del "%tempfile%" /f
move "%tempfile%.1" "%tempfile%"
goto EDITCUR
:LDELETE
if "!filename!"=="" goto NOPEN
if !ecmd1! LEQ 0 goto NOLINE
if !ecmd1! GTR !line! goto NOLINE
echo.
if exist "%tempfile%.1" del "%tempfile%.1" /f
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! LSS !ecmd1! echo %%i>>"%tempfile%.1"
if !line! GTR !ecmd1! echo %%i>>"%tempfile%.1"
)
if exist "%tempfile%" del "%tempfile%" /f
move "%tempfile%.1" "%tempfile%"
goto EDITCUR
:LEDIT
if "!filename!"=="" goto NOPEN
if !ecmd1! LEQ 0 goto NOLINE
if !ecmd1! GTR !line! goto NOLINE
echo.
if exist "%tempfile%.1" del "%tempfile%.1" /f
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! EQU !ecmd1! echo !ecmd1!: %%i
)
set /p cLine="!ecmd1!: "
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
if !line! LSS !ecmd1! echo %%i>>"%tempfile%.1"
if !line! EQU !ecmd1! echo ^%cLine%>>"%tempfile%.1"
if !line! GTR !ecmd1! echo %%i>>"%tempfile%.1"
)
if exist "%tempfile%" del "%tempfile%" /f
move "%tempfile%.1" "%tempfile%"
goto EDITCUR
:CREATENEW
echo.
echo 创建文本 - !filename!
echo.
set /a line=0
if exist "%tempfile%" del "%tempfile%" /f
set editstate=CREATE_NEW
:CRTPARA
set /a line=!line!+1
set /p cLine="%line%: "
if "%cLine%"=="%ctrcmd%" goto CONTROL
echo ^%cLine%>>"%tempfile%"
goto CRTPARA
:EDITOLD
if not exist "!filename!" (
echo 文件 '!filename!' 不存在。
goto CONTROL
)
if exist "%tempfile%" del "%tempfile%" /f
for /f "tokens=*" %%i in (!filename!) do echo %%i>>"%tempfile%"
:EDITCUR
echo.
echo 编辑文本 - !filename!
echo.
set /a line=0
for /f "tokens=*" %%i in (%tempfile%) do (set /a line=!line!+1
echo !line!: %%i)
goto CONTROL
:EOF
if exist "%tempfile%" del "%tempfile%" /f
set tempfile=
set ctrcmd=
set filename=
set editstate=
set line=
set cLine=
set ecmd=
set ecmdf=
set ecmd1=
[