[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

补充以上电子书的最后一页——中文翻译

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
翻译: Rice-Fans      若有不妥之处还望大家指出来,注释不敢冒昧加上!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

>>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命令的多种变化方式可以达到该目的,例如:
---------------------------------for /f-----------------------------------------
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 ...
---------------------------------for /f-----------------------------------------

>>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).
然而,只有最后一种方法(用到more命令的)在windows nt,2000,xp和2003中能够得到一致的结果。第一种方法,不能有效的识别unicode编码文件,同时,如果文件名含有空格时,usebackq开关必须打开。第二种方法,用到type命令,同样在windows nt,2000,xp和2003中,不能有效识别unicode编码文件,同时,要求输入的文件内容不能以位序标志(BOM)开头.

>>In all the examples, assume the contents of of the file numbers.txt to be:
在如下所有的例子中,假设numbers.txt文件输入以下内容:
----------------------numbers.txt--------------------------
one
two
three
four
five
six
seven
eight
nine
ten
----------------------numbers.txt--------------------------

>>Displaying the first line,This example prints one.
显示第一行,打印第一行的例子如下:
------------------------1.bat------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS                  
set "first="                                          
for /f "delims=" %%a in ('more ^< numbers.txt') do (     
                                                     
  if not defined first set first=%%a                    
)                                                      
echo/%first%                                             
pause>nul
------------------------1.bat-------------------------------------------

>>Displaying the first X lines,This example prints one, two and three.
显示前几行,打印第一,二,三行的例子如下:
------------------------123.bat-------------------------------
@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                           
)                                                   
pause>nul
------------------------123.bat-------------------------------

>>Displaying the last line,This example prints ten.
显示最后一行,打印第十行的例子如下:
------------------------10.bat--------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS                             
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%                                                      
pause>nul
------------------------10.bat--------------------------------------------

>>Displaying the last X lines,This example prints nine and ten.
显示最后x行,打印第9,10行的例子如下:
------------------------lastx.bat-----------------------------------------------
@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                                                         
)                                                                  
pause>nul                                                           
------------------------lastx.bat------------------------------------------------

>>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.
显示第n行,打印第三行.
注意启用more命令的扩展功能(/e)开关,同时,ship选项过去常用于for /f命名中,但是当设定的数目小于实际值时,将导致失败
------------------------Nth.bat-----------------------------------------------
@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%                                                   
pause>nul                                                      
------------------------Nth.bat------------------------------------------------

>>Displaying the Nth line plus X number of lines,This example prints five and six.
显示第n+x行,打印第5,6行的例子如下:
------------------------x+Nth.bat--------------------------------------------------
@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                                   
)                                                         
pause>nul
------------------------x+Nth.bat----------------------------------------------------

TOP

虽然不是,但是该链接地址教程后还有此页,以求完整而已。
原文为
  +Batch Function Library
    +Batch Howto's
            - Reading Files  (以上为此页内容)
     ..............
可能还有续集吧,权且以后找到翻译再补充。提前祝大家新年好!



   .

TOP

返回列表