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

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
)

时间: 2024-08-31 16:24:34

批处理提取不同行上的内容的代码_DOS/BAT的相关文章

用批处理程序修改 计算机名 工作组 计算机描述的代码_DOS/BAT

复制代码 代码如下: @echo off echo 骏龙包装计算机名称.计算机描述.工作组修改批处理程序Echo pause cls :set/p id=请输入本机网段号: :set/p ip=请输入本机IP地址: :netsh interface ip set address name="本地连接" source=static addr=192.168.%id%.%ip% mask=255.255.255.0 set /p name=请输您的主机出厂S/N编号: reg add &q

利用批处理实现文件复制并压缩的实现代码_DOS/BAT

批处理 复制并压缩文件的实现代码 复制代码 代码如下: @echo onrem 把源路径赋值给spset sp=D:/JavaWorkSpace/huayu_bbsrem 下面获取当前日期,并调用拷贝过程for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value^|findstr /i "LocalDateTime"') do (call :doit %%a)pauseexit :doit

批处理实现的文字的飞入+变色效果代码_DOS/BAT

复制代码 代码如下: @echo off&setlocal enabledelayedexpansion&cls&color 0f&title Welcome to CN-DOS! mode con: cols=30 lines=3 set "a= PC-X69" set b= set "c=12345689abcde" echo\ :c for /l %%a in (6,1,28) do ( set /a d=%random%%%1

批处理利用HOSTS文件(屏蔽,加速)网站的代码_DOS/BAT

复制代码 代码如下: @echo off&setlocal&cls echo/&echo\&echo=&echo]&echo[&echo+ :start set/p a=选择模式(1:屏蔽网站 2:加速访问 3:删除被屏蔽的网站 4:退出): if not defined a goto start if %a% equ 1 (echo/&goto a) if %a% equ 2 (echo/&goto b) if %a% equ 3

推荐一篇批处理最完整人性化教程第1/3页_DOS/BAT

这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字就能看懂,就能学到知识.写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如果爱可以让事情变的更简单,那么就让它简单吧!看这篇教程的方法,就是慢!慢慢的,如同品一个女人.一杯茗茶,你会发现很多以前就在眼前的东西突然变的很遥远,而有些很遥远的东西却又突然回到了眼前.  先概述一下批处理是个什么东东.批处理的定义,至今我也没能给出一个合适的----众多高手们也都没给出----反正我不知道----看了我也不一定信服----我是个

Dos批处理编写一键清理系统垃圾的bat代码_DOS/BAT

del 命令的参数 /F 强制删除只读文件. /S 从所有子目录删除指定文件. /Q 安静模式.删除全局通配符时,不要求确认. rd 命令的参数 /s 除目录本身外,还将删除指定目录下的所有子目录和文件.用于删除目录树. /q 安静模式 /s 删除目录树时不要求确认. 代码一 @echo off & title 清理系统垃圾 del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*.mp3 del /f /s /q %system

批处理按要求将字符串分段输出的实现代码_DOS/BAT

一.要求用批处理随机输出200个字符到1.txt中的一行,内容类同如下: 代码: xh45q3ma+remgofm54sevhrna4g5r8pl9cjardezqjwj3m8itamh0a4itzd6jz8cmrfmibmhr0wmccyb8qnp2qh4rvlwff6yr2ez4eo063u6viy7ppw+nzxaxe8vrm190eei_0dwx5e2kxbds-ae6e96c9_i9glw8mz+uf6uienx9od8bktfnjlwlqu-e1rcj33_+4bz 二.要求用批处理

批处理bat命令 获取当前盘符和当前目录和上级目录的代码_DOS/BAT

批处理命令获取当前盘符和当前目录 %~d0 是当前盘符 %cd% 是当前目录 可以用echo %cd%进行打印测试 以下例子是命令行编译Visual Studio编写的程序: 复制代码 代码如下: @echo off set b=%cd% //将当前目录保存到参数b中,等号前后不要有空格 C: cd program files cd microsoft visual studio cd common cd msdev98 cd bin msdev "%b%\test.dsp" /MAK

用批处理生成网页文件并打开的代码_DOS/BAT

非常的强啊,将下列内容用记事本做成bat文件,然后运行即可 复制代码 代码如下: @echo off  del CMD命令速查手册.htm >nul  echo.===============================  echo......请稍候,构造htm文件中.....  echo.===============================   >CMD命令速查手册.htm echo ^<head^>  >>CMD命令速查手册.htm echo ^<