批处理提取不同行上的内容的代码

所属分类: 脚本专栏 / DOS/BAT 阅读数: 724
收藏 0 赞 0 分享
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
)
更多精彩内容其他人还在看

DOS 命令字典

DOS 命令字典
收藏 0 赞 0 分享

用replace实现替换正在使用的文件

用replace实现替换正在使用的文件
收藏 0 赞 0 分享

黑客基础之DOS (最齐全)超强推荐

黑客基础之DOS (最齐全)超强推荐
收藏 0 赞 0 分享

bat运行时不弹出那个黑框框的完美解决方案

bat运行时不弹出那个黑框框的完美解决方案
收藏 0 赞 0 分享

收藏的比较完整的批处理教程第1/2页

收藏的比较完整的批处理教程
收藏 0 赞 0 分享

批处理版的SC

批处理版的SC
收藏 0 赞 0 分享

BAT文件语法和技巧(bat文件的编写及使用)

批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件
收藏 0 赞 0 分享

用 Net 命令使局域网文件批量同步更新

用 Net 命令使局域网文件批量同步更新
收藏 0 赞 0 分享

如何用DOS命令删除顽固文件夹啊

如何用DOS命令删除顽固文件夹啊
收藏 0 赞 0 分享

dos下删除文件夹和文件的方法

dos下删除文件夹和文件的方法
收藏 0 赞 0 分享
查看更多