佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 批處理提取不同行上的內(nèi)容的代碼

批處理提取不同行上的內(nèi)容的代碼

熱門標(biāo)簽:銀行業(yè)務(wù) 智能手機 網(wǎng)站文章發(fā)布 檢查注冊表項 美圖手機 服務(wù)器配置 鐵路電話系統(tǒng) 呼叫中心市場需求
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
)

標(biāo)簽:新疆 河南 樂山 沈陽 滄州 長治 紅河 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《批處理提取不同行上的內(nèi)容的代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    翼城县| 沙雅县| 芦山县| 四子王旗| 图们市| 南开区| 全州县| 青田县| 满城县| 宁强县| 沽源县| 建湖县| 尉氏县| 高雄县| 晋江市| 榕江县| 凌云县| 宣威市| 思茅市| 邵东县| 满城县| 定州市| 乌什县| 松滋市| 盐亭县| 洞口县| 锡林浩特市| 大石桥市| 库伦旗| 镇巴县| 天长市| 微博| 进贤县| 筠连县| 阿瓦提县| 绥阳县| 镇坪县| 汕尾市| 新竹市| 固安县| 太原市|