标题: [文本处理] [分享]批处理两两合并文件并取首行内容作为新文件名 [打印本页]
作者: Batcher 时间: 2021-9-27 17:47 标题: [分享]批处理两两合并文件并取首行内容作为新文件名
【问题】
在一个文件夹里,批处理合并001.txt + 002.txt 合并后的新文件重命名为001.txt里的第一行文字,合并003.txt + 004.txt 合并后的新文件重命名为003.txt里的第一行文字,合并005.txt + 006.txt 合并后的新文件重命名为005.txt里的第一行文字......
作者: Batcher 时间: 2021-9-27 17:47
test1.bat- @echo off
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
- set "NewFolder=合并结果"
- if not exist "%NewFolder%" (
- md "%NewFolder%"
- )
- for /l %%i in (1001,2,1999) do (
- set "FileA=%%~ni"
- if exist "!FileA:~-3!.txt" (
- set /a FileB=FileA+1
- set /p NewFile=<"!FileA:~-3!.txt"
- copy /b !FileA:~-3!.txt+!FileB:~-3!.txt "%NewFolder%\!NewFile!.txt"
- )
- )
复制代码
作者: Batcher 时间: 2021-9-27 18:16
test2.bat- @echo off
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
- set "NewFolder=合并结果"
- if not exist "%NewFolder%" (
- md "%NewFolder%"
- )
- set "n=0"
- for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
- set /a n+=1
- set /a m=n%%2
- if !m! equ 0 (
- set FileMerge="!FileMerge!"+"%%i"
- copy /b !FileMerge! "%NewFolder%\!NewFile!.txt"
- ) else (
- set "FileMerge=%%i"
- set /p NewFile=<"%%i"
- )
- )
复制代码
作者: qixiaobin0715 时间: 2021-9-27 20:41
- @echo off
- setlocal enabledelayedexpansion
- cd /d "%~dp0"
- set "NewFolder=合并结果"
- if not exist "%NewFolder%" md "%NewFolder%"
- for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
- if not defined n (
- set /p NewFile=<"%%i"
- set str=%%i
- set n=true
- ) else (
- copy !str!+%%i "%NewFolder%\!NewFile!.txt"
- set n=
- )
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2021-9-28 08:25
蹭个热度。通常情况下,如果路径中含有空格的话,执行dos命令时,含有空格的路径应当使用双引号。有2个命令可以例外,那就是标记路径的命令pushd和目录跳转命令cd,当它们后面含有空格路径时,可不加双引号直接使用。
作者: Batcher 时间: 2021-9-28 09:08
回复 5# qixiaobin0715
老版本的系统里面不加双引号会出错
作者: qixiaobin0715 时间: 2021-9-28 21:20
回复 6# Batcher
win7 win10可以,没有xp系统。
作者: Batcher 时间: 2021-9-28 21:26
回复 7# qixiaobin0715
除了PC系统,还有一些老的Server版本的系统也是需要加双引号的。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |