Batch processing code that extracts the content on the _dos/bat

Source: Internet
Author: User
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) would give consistent results across Windows NT, XP and 2003. The 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 XP, and 2003 if the input File does not begin with a bit order mark (BOM).

In the examples, assume the contents of the file Numbers.txt to be:-

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

displaying the

This example prints one.

@echo Off & setlocal enableextensions
Set "first="
for/f "delims="%%a in (' More ^< numbers.txt ') do (
If not defined the First=%%a
)
echo/%first%

displaying the 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 This instead of using the more command ' s/e switch, the skip option could have been and the used command, how Ever, this fails is it's 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
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.