中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 【共享】文件 网络 系统 批函数若干
« [1] [2] »
作者:
标题: 【共享】文件 网络 系统 批函数若干 上一主题 | 下一主题
plp626
银牌会员

钻石会员


积分 2278
发帖 1020
注册 2007-11-19
状态 离线
『第 16 楼』:  Reading Files

This page shows how to read specific lines from a text file. There are many ways to have the for /f command read the input file, 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
)



山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
2008-4-27 03:06
查看资料  发短消息 网志   编辑帖子  回复  引用回复
learner0
初级用户





积分 108
发帖 49
注册 2008-2-26
状态 离线
『第 17 楼』:  

顶一下,收藏..

2008-4-27 11:21
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
« [1] [2] »
请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: