跳到主要内容

合并文本

本篇讲合并文本内容,如交叉合并、去除重复内容行和空行、行首行尾加特定字符、加指定分割线等操作。

交叉合并两个文本文件的内容:

@Echo Off
title 交叉合并文本内容

:GTBegin
@echo 默认合并目录下 a.txt 和 b.txt 两个文本文件到 c.txt,确定继续?(y/n)
set /p GTConfirm=
if "%GTConfirm%"=="y" goto GTYES
if "%GTConfirm%"=="Y" goto GTYES
if "%GTConfirm%"=="n" goto GTNO
if "%GTConfirm%"=="N" goto GTNO
echo 请输入正确的指令
pause >nul
goto GTBegin

:GTYES
pushd %cd%
for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
findstr /r /c:"[^ ]" "%%a">#
move # "%%a"
)
popd
For /F "tokens=1* delims=:" %%i In ('Findstr /n .* b.txt') Do Set "_%%i=%%j"
(For /F "delims=" %%i In ('Findstr /n .* a.txt') Do (
Set "Str=%%i"& Set /A Count+=1
SetLocal EnableDelayedExpansion
For %%j In (!Count!) Do (
Echo,!Str:*:=!
Echo,!_%%j!
)
EndLocal
))>c.txt
Start c.txt
echo. :) 搞定
pause >nul
exit

:GTNO
exit


合并所有文本内容,并去除重复内容行和空行:

@echo off
title 合并文本删除重复行

:GTBegin
@echo 默认合并目录下所有文本文件到 all.txt,确定继续?(y/n)
set /p GTConfirm=
if "%GTConfirm%"=="y" goto GTYES
if "%GTConfirm%"=="Y" goto GTYES
if "%GTConfirm%"=="n" goto GTNO
if "%GTConfirm%"=="N" goto GTNO
echo 请输入正确的指令
pause >nul
goto GTBegin

:GTYES
for /f "delims=" %%a in ('dir /a-d /b /on *.txt') do (
for /f "delims=" %%b in ('type "%%a"') do (
if not defined %%b set %%b=Def & >>$ echo,%%b
)
)
move $ "all.txt"
echo. :) 搞定
pause >nul
exit

:GTNO
exit


行首行尾加特定字符,预置好字符,拖入要处理的文本到该批处理文件图标上, 这里加 <p></p>

@echo off
@REM @Author: [email protected]
@REM @Date: 2016-04-06 11:22:44
title 文本行首尾加字符
:GTCONTINUE
cls
for /f "delims=" %%a in ('type "%~1"') do echo ^<p^>%%~a^</p^>>>"%~dp1%~n1-temp%~x1"
goto GTCONTINUE

将大批量图片放置在服务器,要组成图片 HTML 代码标签, 拖入要处理的文本到该批处理文件图标上:

@echo off
@REM @Author: [email protected]
title 文本内容行首尾加字符
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('type "%~1"') do set FilePath="%~dp1%~n1-done%~x1"
if exist "%FilePath%" DEL/q "%FilePath%"
cd.>%FilePath%
for /f "delims=" %%a in ('type "%~1"') do echo ^<img src=^"%%~a^" /^> >> "%FilePath%"
set "FilePath=%FilePath:"=%"
start %FilePath%
exit


加指定分割线,和将要合并的文本处于同一目录,然后执行。

先设定分割线 >>all.temp echo ========================================================================

以及前后换行 >>all.temp echo,,这里的 , 是空格,基础篇里有说。

@echo off
setlocal enabledelayedexpansion
title 合并文本(加指定分割线)

:GTBegin
@echo 默认合并目录下所有文本文件到 all.txt,确定继续?(y/n)
set /p GTConfirm=

set varTimeTemp1=%time: =0%
set varTimeTemp2=%varTimeTemp1::=%
set varTime=%varTimeTemp2:~0,6%
set varDate=%date:~0,4%%date:~5,2%%date:~8,2%%varTime%

if "%GTConfirm%"=="y" goto GTYES
if "%GTConfirm%"=="Y" goto GTYES
if "%GTConfirm%"=="n" goto GTNO
if "%GTConfirm%"=="N" goto GTNO
echo 请输入正确的指令
pause >nul
goto GTBegin

:GTYES

rem 自然合并所有,/b/od 开关可按指定规则按文件名排序进行合并 for /f %%i in ('dir /b *.txt') do ()

>all.temp type nul
for %%i in (*.txt) do (
echo 正在处理文件:%%i
>>all.temp type "%%i"
>>all.temp echo,
>>all.temp echo,
>>all.temp echo ========================================================================
>>all.temp echo,
>>all.temp echo,
)
move /y all.temp all-%varDate%.txt

echo. :) 搞定
pause >nul
exit

:GTNO
exit

rem /O 用分类顺序列出文件。
rem N 按名称(字母顺序) S 按大小(从小到大)
rem E 按扩展名(字母顺序) D 按日期/时间(从先到后)
rem G 组目录优先 - 反转顺序的前缀

更新日志:

  • 2017-05-26