当前位置: 首页 > 图文教程 > 脚本技术 > DOS/BAT > 批处理提取不同行上的内容的代码

DOS/BAT
用 Net 命令使局域网文件批量同步更新
如何用DOS命令删除顽固文件夹啊
dos下删除文件夹和文件的方法
Windows 2000下用DOS命令删除文件夹
批处理编程- -介绍
Ping命令详解 精简实用
DOS下FTP命令(FTP命令全集)
几个bat例子文件
最强的用批处理编写的病毒
批处理经典入门教程!(从不懂到高手)
Ghost常用参数详解
很详细的Ghost所有运行错误代码完全大揭秘
GHOST参数、命令操作指南
用批处理实现电脑自动关机
如何让批处理程序启动的时候最小化
如何让“>”显示出来批处理(dos)中的转义符号
批处理经常用到的bat推荐下
经过整理的一些dos/bat批处理教程
妙用脚本和批处理清除电脑中的痕迹
又一篇不错的批处理bat学习教程

DOS/BAT 中的 批处理提取不同行上的内容的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 67 ::
收藏到网摘: n/a

这个网页显示如何阅读的具体行从一个文本文件。有许多方法可以有供/ F命令读取输入文件 for instance:-
for /f "delims=" %%a in (input.txt) do ...
for /f "delims=" %%a in ('type input.txt') do ...
for /f "delims=" %%a in ('more ^< input.txt') do ...
However, only the last method (using the more command) will give consistent results across Windows NT, 2000, XP and 2003. The first method does not recognise unicode files. Also, the usebackq switch must be used if the input filename contains spaces. The second method, using the type command, also fails to recognise unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM).
In all the examples, assume the contents of of the file numbers.txt to be:-
one
two
three
four
five
six
seven
eight
nine
ten
Displaying the first line
This example prints one.
@echo off & setlocal ENABLEEXTENSIONS
set "first="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
if not defined first set first=%%a
)
echo/%first%
Displaying the first X lines
This example prints one, two and three.
@echo off & setlocal ENABLEEXTENSIONS
set "lines=3"
set i=-1
set "ok="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)
Displaying the last line
This example prints ten.
@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%
Displaying the last X lines
This example prints nine and ten.
@echo off & setlocal ENABLEEXTENSIONS
set "lines=2"
for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
echo/%%a
)
Displaying the Nth line
This example prints three. Note that instead of using the more command's /e switch, the skip option could have been used with the for /f command, however, this fails is it is set to any number less than one.
@echo off & setlocal ENABLEEXTENSIONS
set LineNo=3
set "line="
set/a LineNo-=1
for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
if not defined line set "line=%%a"
)
echo/%line%
Displaying the Nth line plus X number of lines
This example prints five and six.
@echo off & setlocal ENABLEEXTENSIONS
set start=5
set "lines=2"
set/a i=-1,start-=1
set "ok="
for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)