Board logo

标题: [原创] Ritchie Lawrence 批处理函数库中英文版 [打印本页]

作者: Batcher    时间: 2009-1-12 09:29     标题: Ritchie Lawrence 批处理函数库中英文版

[attach]1338[/attach]
[attach]868[/attach]

版主CrLf于20120109对以下几个函数提出的更改建议(主要针对中文版操作系统)
GetDate.CMD
GetMAC.CMD
GetTime.CMD
Timer.CMD
Uptime.CMD
http://bbs.bathome.net/thread-3056-2-1.html#pid100296

应用实例:

批处理获取指定天数之前的日期
http://www.bathome.net/thread-3330-1-1.html

批处理删除指定天数日期之前的文件
http://www.bathome.net/thread-3334-1-1.html

批处理删除指定天数日期之前的文件夹
http://www.bathome.net/thread-3503-1-1.html

批处理从FTP下载指定天数之前的文件
http://www.bathome.net/thread-4010-1-1.html

批处理从FTP下载指定小时之前的文件
http://www.bathome.net/thread-5475-1-1.html

批处理删除指定日期的分钟数之前的文件
http://www.bathome.net/thread-5648-1-1.html

批处理计算两个日期时间的差值
http://www.bathome.net/thread-11128-1-1.html

批处理计算指定分钟数之前的日期时间
http://www.bathome.net/thread-13259-1-1.html

批处理计算指定秒数之后的日期时间
http://www.bathome.net/thread-14413-1-1.html

批处理获取上个月的最后一天
http://www.bathome.net/thread-7336-1-1.html

批处理获取上个月的第一天
http://www.bathome.net/thread-3960-1-1.html

获取一个小时前的日期时间
http://bbs.bathome.net/thread-29303-1-1.html#pid145870

BAT批量处理文件名unix时间为正常时间
http://bbs.bathome.net/thread-63711-1-1.html#pid259047
作者: RiceFans    时间: 2009-1-12 10:42     标题: 补充以上电子书的最后一页——中文翻译

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
翻译: 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----------------------------------------------------
作者: Batcher    时间: 2009-1-12 12:03     标题: 回复 2楼 的帖子

那个“最后一页”不是函数库的内容吧?
作者: RiceFans    时间: 2009-1-12 13:08

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



   .
作者: Batcher    时间: 2009-1-12 13:36     标题: 回复 4楼 的帖子

本帖标题为“批处理函数库中英文版”,目的仅仅是收集“函数库”。
后面加上的那个跟“函数库”没什么关系吧?“以求完整”的说法可能不是太合适。
不过,仍感谢分享。
作者: freeback    时间: 2009-3-22 17:15

只是有些还是看不懂,等回去研究了,不懂的在回来问大家
作者: apple601601601    时间: 2009-5-17 22:42

好东西啊!!!!!!!
作者: neity    时间: 2009-9-27 22:13

好,谢谢。。。。。。。。。。。。。。。。。
作者: vv4474    时间: 2009-9-28 00:27

太有创造力了……十二万分感谢楼主……
作者: firefly    时间: 2011-3-29 20:23

谢过,拿来学习了!
作者: zhoutingting    时间: 2011-4-5 11:31

确实不错的啊呀
作者: zhoutingting    时间: 2011-4-5 11:32

值得学习了!楼主辛苦了解
作者: aosen007    时间: 2011-4-6 12:38

下载看看,我也要学习学习一番。。。。。。
作者: 1e3e    时间: 2011-7-20 11:01

非常感谢分享哟,楼主真是好人
作者: garyng    时间: 2011-8-23 15:24

下载了~
学习中~
谢谢楼主~
作者: zaixinxiangnian    时间: 2011-8-23 17:37

下载下来大概看了下。太难了,,,,什么是函数都不知道?
作者: find    时间: 2012-1-9 16:15

回复 16# zaixinxiangnian


先找些基础教程看看吧
作者: Batcher    时间: 2012-1-9 16:24

版主CrLf于20120109对以下几个函数提出的更改建议(主要针对中文版操作系统)
GetDate.CMD
GetMAC.CMD
GetTime.CMD
Timer.CMD
Uptime.CMD
作者: 1094454852    时间: 2012-1-21 11:58

感谢分享...!!
作者: Sirius    时间: 2012-3-16 10:39

主要原因是由于Microsoft把0~9点之间小时显示前面的0省略了,无“if 1%hh% LSS 20 set hh=0%hh%”会有缺陷。GetTime如下好些:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: 作者:Ritchie Lawrence, 2007-05-12. 版本 1.3
::
:: 功能:把本地系统时间赋值给参数 1 到 4。
::       适用于 NT4/2000/XP/2003
::
:: 用法:Call GetTime hh mm ss tt
::
:: 参数:%1 该变量用于接收小时,两位,00 到 23(引用调用)
::       %2 该变量用于接收分钟,两位,00 到 59(引用调用)
::       %3 该变量用于接收秒钟,两位,00 到 59(引用调用)
::       %4 该变量用于接收百分之一秒,两位,00 到 99(引用调用)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetTime
setlocal ENABLEEXTENSIONS
for /f "tokens=1-4 delims=0123456789" %%a in ("%time%")do set delims=%%a%%b%%c%%d
for /f "tokens=1-4 delims=%delims: =% " %%a in ("%time%")do (
  set hh=%%a&set mm=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
endlocal&set %1=%hh%&set %2=%mm%&set %3=%ss%&set %4=%cs%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
作者: Batcher    时间: 2012-3-16 19:13

回复 20# Sirius


具体的缺陷是什么?你给个测试用例我试试
作者: 秋风·飞扬    时间: 2012-5-2 11:55

要深入,要学习,要各种深入,要各种学习
作者: mq0036    时间: 2014-1-24 21:41

想请教各位大神们,帮我说明一下算法;特别是如下两行代码:
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
各个数字代表什么意思,又是怎么算出来的,为什么要使用这个数字?
作者: saturn    时间: 2017-2-14 15:35

好强大, 作者辛苦了
作者: ANSL    时间: 2022-11-4 20:40

特别特别有用!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2