如何解决批处理文件可将多个文本文档附加到一个文档
我创建了一个批处理文件,该批处理文件应执行多项操作,包括将某种格式的所有文本文档附加到一个文本文件中,前提是目录中存在多个此格式的文本文件。此部分代码如下:
: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变量大于1,似乎代码也会在到达if语句时崩溃。 if语句中的任何代码均不执行,并且if语句之后的任何代码均不执行。是否有任何语法或其他错误可能导致这种情况?
解决方法
除了我的评论。我假设您不是真正打算输入*.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"
,
仅出于提供替代选择的目的,它使用xcopy检查文件计数,并使用copy合并它们。
PushD "%location%\Output" 2>NUL && For /F %%G In ('""%__AppDir__%xcopy.exe" "UAV_camera_coords_*.txt" . /SQL"')Do If %%G Gtr 1 Copy /Y /B "UAV_camera_coords_*.txt" "..\UAV_camera_coords_all.txt">NUL & PopD
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。