- :: 功能:
- :: 处理当前目录下所有 .txt 文件
- :: 1) 删除文件末尾的空行、空格行、制表符行
- :: 2) 如果最后一行不足 80 个字符,则删除
- ::
- :: 思路:
- :: 1) 计算非空行的行数
- :: 2) 用 "findstr /n" 输出到 .tmp
- :: 3) 只计算 .tmp 的非空行的行数,这样就知道哪行是最后一行(非空行)了
-
- @echo off
- rem 统计文件总数
- set /a Total = 0, Finished = 0
- for %%n in (*.txt) do set /a Total += 1
-
- rem 退格符
- set bs=
-
- for /f "delims=" %%F in ('dir /a:-d /b *.txt') do (
- rem 计算非空行的行数
- set /a Lines = 0, Finished += 1
- for /f "usebackq eol=" %%n in ("%%F") do set /a Lines += 1
-
- rem 使空行不丢失
- findstr /n .* "%%F" > .tmp
-
- (
- for /f "tokens=* delims=0123456789" %%L in (.tmp) do (
- rem 不为空行,则对行数进行计数
- for /f "tokens=1,2" %%1 in ("%%L") do (
- if "%%1%%2" neq ":" set /a Lines -= 1
- )
-
- set Str=%%L
- SetLocal EnableDelayedExpansion
- if !Lines! gtr 0 (
- echo,!Str:~1!
- EndLocal
- ) else (
- if !Lines!==0 (
- if "!Str:~80,1!" neq "" echo !Str:~1!
- EndLocal
- set Lines=-1
- ) else (
- EndLocal
- )
- )
- )
- ) > "%%F"
-
- SetLocal EnableDelayedExpansion
- set /p=%bs%已完成 !Finished! 个文件(共 !Total! 个)<nul
- EndLocal
- )
-
- del .tmp
复制代码
[ 本帖最后由 del 于 2010-5-31 13:28 编辑 ] |