iT邦幫忙

0

如何用bat檔案自動修改ini內容

  • 分享至 

  • xImage

我有一個ini檔案內容名稱tk.ini
123456
223
3332
5678
8906
9999999
#我想把ini檔案內容第五行和第六行刪除

以下是腳本無法執行,麻煩大大幫幫忙
bat檔案
@echo off
{
for %%i in (5,6)do set #%%i=y
(for /f "tokens=1* delims=:" %%i in ('findstr/n .* "%USERPROFILE%\Desktop\tk.ini"')do (
if not defined #%%i echo;%%j
))>"%USERPROFILE%\Desktop\tk.ini"
}

可以請大大教教我怎麼修改bat腳本嗎

給你另一個思維,先不要將結果寫入你的tk.ini。先寫到另一個temp檔案上。
處理完成後再將其copy過來並刪除temp(可先保留檢查)
sam0407 iT邦大師 1 級 ‧ 2019-02-26 12:05:04 檢舉
有個unix指令叫sed,正適合處理樓主的問題,網路上找的到windows版本
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
jeles51
iT邦研究生 3 級 ‧ 2019-02-26 10:21:10

yoching所說,先將結果另存,確認後再改回原本的檔名.
Powershell可做到.
來源參考:這裡

範例:
將 bb.txt 文字內容,去掉第3~7行,剩下的檔案存到cc.txt

作法:
準備 bb.txt / skip.ps1

#bb.txt內容

1
2
3
4
5
6
7
8
9
10

#skip.ps1內容

#取bb.txt內容,去除3~7行,另存為cc.txt
get-content bb.txt | where {$_.readcount -lt 3 -or $_.readcount -gt 7} | set-content cc.txt

指令:

powershell -File skip.ps1

結果會產出 cc.txt

#cc.txt內容(自動產出)

1
2
8
9
10

我要發表回答

立即登入回答