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

DOS/BAT
Bootcfg 配置、查询或更改 Boot.ini 文件设置
Assoc显示或修改文件名扩展关联
at计划在指定时间和日期在计算机上运行命令和程序
使用 atmadm 来显示 ATM 适配器上传入和传出呼叫的统计信息
Attrib 显示、设置或删除指派给文件或目录的只读、存档、系统以及隐藏属性
可以使用的批处理参数集合
批处理下使用筛选器的函数
Getmac返回计算机中所有网卡的媒体访问控制 (MAC) 地址以及每个地址的网络协议列表
Cacls 显示或修改任意访问控制列表 (DACL) 文件
Call 从一个批处理程序调用另一个批处理程序,并且不终止父批处理程序。
Chcp 显示活动控制台代码页数量,或更改该控制台的活动控制台代码页
Chdir (Cd) 显示当前目录的名称,或更改当前的文件夹
Chkntfs 显示或指定在启动计算机时计划的自动系统检查是否在 FAT、FAT32 或者 NTFS 卷上运行。
Cipher 在 NTFS 卷上显示或改变文件的加密
Cls 清除命令提示符窗口
Cmd 启动命令解释器 Cmd.exe 的新实例
Cmstp 安装或删除“连接管理器”服务配置文件
color 对于当前会话,更改命令提示窗口的前景和背景色
配置命令提示符的方法
Comp 逐字节地比较两个文件或几组文件的内容

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 83 ::
收藏到网摘: 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
)