首页 > 解决方案 > 将多个文本文档附加到一个文档的批处理文件

问题描述

我创建了一个批处理文件,它应该做几件事,包括将某种格式的所有文本文档附加到一个文本文件中,前提是目录中有多个这种格式的文本文件。这部分代码如下:

:multiple
SET /a count=0
ECHO.> "%location%\UAV_camera_coords_all.txt"
FOR /r "%location%\Output" %%G in ("UAV_camera_coords_*.txt") do set /a count+=1
IF %count% GTR 1 (
FOR /r "%location%\Output" %%G in ("*.txt") DO (
SET file=%%~G
TYPE "%file%">>"%location%\UAV_camera_coords_all"
)
GOTO :end

即使 count 变量大于一,代码在到达 if 语句时似乎也会崩溃。if 语句中的任何代码都没有被执行,实际上也没有应该在 if 语句之后执行的代码。是否有任何语法或其他错误可能导致此问题?

标签: batch-filesyntax

解决方案


除了我的评论。我假设您并没有真正计划输入的输出,*.txt而只是UAV_camera_coords_*.txt.. 如果没有,请随时将其更改回*.txt

@echo off
for /f "tokens=1,*" %%i in ('dir /s "%location%\UAV_camera_coords_*.txt" ^| findstr "File(s)"') do set cnt=%%i
if %cnt% gtr 1 (
    for /f %%a in ('dir /b /s "%location%\UAV_camera_coords_*.txt"') do type "%%~a"
)>"%location%\UAV_camera_coords-all.txt"

请注意,我将输出文件名更改为-all,而不是_all因为如果它也是一个文件,它会将文件键入到自身上txt

在我意识到您正在递归搜索目录之前,编辑添加回原始答案:

@echo off
for /f "tokens=1,*" %%i in ('dir "UAV_camera_coords_*.txt" ^| findstr "File(s)"') do if %%i gtr 1 type "UAV_camera_coords_*.txt">>"%location%\UAV_camera_coords_all"

推荐阅读