• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >DOS/BAT > 批处理提取不同行上的内容的代码

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了批生产记录的内容,批处理读取文件内容,批处理替换文件内容,检验批质量验收内容,批处理修改文件内容等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
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
) </div>

您可能想查找下面的文章:

  • 批处理提取不同行上的内容的代码
  • 批处理应用:根据文件内容进行重命名操作
  • 批处理应用根据文件内容进行重命名操作

相关文章

  • 批处理下使用筛选器的函数
  • 运行cmd直接进入指定目录下的命令
  • 调用maven命令的BAT脚本分享
  • MS-DOS版本的历史介绍
  • 开通局域网共享(访问本机要填用户名和密码)的注册表和批处理
  • 批处理应用 "添加/删除程序"修复
  • 用bat实现删除系统脚本调试程序
  • bat延时执行命令的另一种方法
  • Del (erase) 删除指定文件
  • DOS下硬件设备的使用与设置

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 删除DOS结尾符的方法
    • Ghost常用参数详解
    • Date命令
    • dos批处理文件中的变量小结
    • 安全工具netsh IPSec使用方法[ip安全策略]
    • windows下通过批处理快速批量更换IP方法
    • DOS批处理高级教程 第二章 DOS循环for命令详解
    • del rd命令行下删除文件不需要确认
    • 通过批处理实现删除运行、查找等处的历史记录的代码
    • 系统配置—获得更多常规内存

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有