Windows批处理:将新输出文件保存在新文件夹中,文件名为源目录

s5a0g9ez  于 2022-12-19  发布在  Windows
关注(0)|答案(1)|浏览(150)

当前我在我的子目录中连接多个文本文件,如下所示:

for /D %%J in ("C:\Users\hp\Desktop\Test Annual Reports\*") do (
    > "%%~J\merge.txt" (
        for /F "delims= eol=|" %%I in ('
            dir /B /A:-D-H-S /O:N "%%~J\*.txt" ^| findstr /V /I /C:"merge.txt"
        ') do (
            type "%%~J\%%I"
        )
    )
)

你能告诉我如何修改代码吗?它会将“merge.txt”文件保存到一个新目录中,以它们的源目录命名。例如:

C:\Users\hp\Desktop\Outputdirectory\Subdirectory 2_merged.txt

先谢谢你了!

4szc88ey

4szc88ey1#

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the directories are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=U:\Users\hp\Desktop\Test Annual Reports"
SET "destdir=U:\Users\hp\Desktop\Outputdirectory"
MD "%destdir%">NUL 2>nul

for /D %%J in ("%sourcedir%\*") do (
    > "%destdir%\%%~nxJ_merge.txt" (
        for /F "delims= eol=|" %%I in ('
            dir /B /A:-D-H-S /O:N "%%~J\*.txt" ^| findstr /V /I /C:"merge.txt"
        ') do (
            type "%%~J\%%I"
        )
    )
)

GOTO :EOF

在应用于真实的数据之前,始终根据测试目录进行验证。

唯一真实的的变化是目标文件名是从%%J~nx“名称和扩展名”构建的,其中%%J被解释为文件名。
请注意,从逻辑上讲,merge.txt不应该存在于这个新方法中......

相关问题