簡體   English   中英

Windows Batch:將新的輸出文件保存在新文件夾中,名稱為源目錄

[英]Windows Batch: Save new output files in new folder with name of source directory

目前我在我的子目錄中連接多個文本文件,如下所示:

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

非常感謝您!

@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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM