
我需要使用特定文字編輯特定行下方的行我正在 Windows 8 中使用 .bat 或 .vbs 編輯 .txt 檔案。
例子
Account 1
ABC - Debit 900
- Credit 900
DEF - Debit 800
- Credit 100
我希望輸出是這樣的
Account 1
ABC - Debit 900
ABC - Credit 900
DEF - Debit 800
DEF - Credit 100
答案1
批次編輯意味著重寫文件,所以這可能有效:
:: Q:\Test\2018\04\17\SU_1314377.cmd
@Echo off
SetLocal EnableExtensions EnableDelayedExpansion
Set "File=Accounts.txt"
Set "NewFile=Accounts2.txt"
(For /f "delims=" %%A in (%File%) do (
Set "Line=%%A"
Set "Debit=!Line:*Debit=!"
Set "Credit=!Line:*Credit=!"
If "!Line!" Neq "!Debit!" Call Set "Account=%%Line:Debit!Debit!=%%"
If "!Line!" Neq "!Credit!" Call Set "Line=!Account!Credit!Credit!"
Echo:!Line!
) )> "%NewFile%"
- 此批次透過將目前行中的所有內容替換為「借方/貸方」一詞來提取借方/貸方值(帶有前導空格)。
- 如果目前行包含 Debit,則前面的名稱將儲存到 var Account
- 如果目前行包含 Credit,則使用前面儲存的值重建該行