用批次檔讀取文字檔後將檔案內容中特定文字取代(已解決)

請問各位大大
我現在有一個文字檔
我想把這個檔案內的三行文字取代
請問如果用批次檔要怎麼寫呢
,
"HK_BTWN_FLT": [],
"LL_CATCHES": []

用批次檔讀取文字檔後將檔案內容中特定文字取代(已解決)
檔案內容如下
{
"ACT_ID": 6,
"ACT_DATETIME": "2022-09-29T12:00Z",
"LAT": "-9807.998",
"LON": "+17825.002",
"HK_BTWN_FLT": [],
"LL_CATCHES": []
},
{
"ACT_ID": 6,
"ACT_DATETIME": "2022-10-01T12:00Z",
"LAT": "-2807.998",
"LON": "+17825.002",
"HK_BTWN_FLT": [],
"LL_CATCHES": []
},
{
"ACT_ID": 6,
"ACT_DATETIME": "2022-10-03T12:00Z",
"LAT": "-9807.998",
"LON": "+17825.002",
"HK_BTWN_FLT": [],
"LL_CATCHES": []
}
====================剛剛得到大神指點================

This is a '.ps1' file;
## Tested @[Windows-PowerShell-v5.1];
$myOldStr = @'
,
"HK_BTWN_FLT": [],
"LL_CATCHES": []
'@
$myNewStr = ''
$mySrcFilePath = '.\*.json'
$myDstFilePath = '.\*.json'
(Get-Content $mySrcFilePath -Raw).Replace($myOldStr, $myNewStr) | Set-Content $myDstFilePath -Encoding UTF8
##

=================這是一段powershell指令=================
2023-05-26 12:24 發佈
天才小豪 wrote:
請問各位大大我現在有...(恕刪)


檔案是 Json 格式的... 用 C# 處理比較快和準確!

using Newtonsoft.Json;
利用 JsonConvert.SerializeObject( XXX ) 讀取
再 JsonConvert.DeserializeObject<yyy>() 寫出</yyy>
rone2101 wrote:
C#

感謝大大

但是我不會C#

所以雖然他是json檔沒錯,但我想把他當成一般文字檔處理就好
因為格式已經有了,只是想把裡面一些字拿掉
您好,您好,我想了一下,可以用Notepad++,開啟全部檔案後,用ctrl+h的取代按鈕一次把所有檔案的字串值取代掉,這樣是可行的嗎?
天才小豪
天才小豪 樓主

沒注意到您有提到開啟全部檔案。確實這樣比起一次取代一個檔案來得快,但每天都要手動處理還是覺得累 哈哈~~

2023-05-26 14:57
CONA24

原來是有每日排程批次處理需求,這樣的話確實能夠有個排程的程序監督處理會比較恰當。

2023-05-26 15:02
CONA24 wrote:
您好,您好,我想了一...(恕刪)
報告大大,notepad++是可以做到的
但相比windows10內建記事本來說相對麻煩一些
換行符號需要自己手動輸入,搜尋模式需要選擇延伸
而記事本則只需要將這三行選起來,然後到編輯>取代,再按全部取代即可

如果只有少數檔案要處理,這樣確實很方便,但因為每天要處理,然後有幾十個檔案。
感覺還是很累人

本大爺

去了解一下python, 處理這個比吃滷肉飯還簡單, 跟chatGPT講完要他寫一段python來處理可能花不到2分鐘,也可以每天定時自動

2023-05-27 6:58
天才小豪
天才小豪 樓主

大大您可以提供一下程式碼讓我測試嗎~~謝謝您~~

2023-05-27 8:00
天才小豪 wrote:
但相比windows10內建記事本來說相對麻煩一些
換行符號需要自己手動輸入,搜尋模式需要選擇延伸
而記事本則只需要將這三行選起來,然後到編輯>取代,再按全部取代即可

notepad++ 完全可以做到這個功能

notepad++ 也可以錄 macro
其實這種基本coding問題你就問chatGPT就好了

: windows batch file to replace predefined word in multiple txt file

> Certainly! Here's an example of a Windows batch script that replaces a predefined word in multiple text files:
@echo off
setlocal enabledelayedexpansion

set "search=predefined_word"
set "replace=new_word"

set "folder=C:\path\to\folder"

for %%F in ("%folder%\*.txt") do (
set "output=%%~dpnF_new.txt"
if exist "!output!" del "!output!"

for /f "usebackq delims=" %%A in ("%%F") do (
set "line=%%A"
set "line=!line:%search%=%replace%!"
echo !line! >> "!output!"
)

move /y "!output!" "%%F"
)

echo Replacement complete.


Here's a breakdown of how the script works:

The search variable is set to the word you want to replace, and the replace variable is set to the new word.
The folder variable is set to the path of the folder where the text files are located.
The script loops through all the text files in the specified folder using a for loop.
For each file, a new output file name is generated by appending "_new" to the original file name (%%~dpnF extracts the drive, path, and file name without extension).
If the output file already exists, it is deleted to ensure a fresh replacement.
Another for loop reads each line of the input file.
The line is stored in the line variable, and the predefined word is replaced with the new word using the line replacement syntax !line:%search%=%replace%!.
The modified line is appended to the output file using echo.
Finally, the output file is moved to overwrite the original file (move /y is used to suppress the confirmation prompt).
To use the script, you need to modify the search, replace, and folder variables according to your requirements. Save the script with a .bat extension and run it by double-clicking the file. It will replace the predefined word in all the text files within the specified folder.
Dino M wrote:
@echo off
setlocal enabledelayedexpansion

set "search=predefined_word"
set "replace=new_word"

set "folder=C:\path\to\folder"

for %%F in ("%folder%\*.txt") do (
set "output=%%~dpnF_new.txt"
if exist "!output!" del "!output!"

for /f "usebackq delims=" %%A in ("%%F") do (
set "line=%%A"
set "line=!line:%search%=%replace%!"
echo !line! >> "!output!"
)

move /y "!output!" "%%F"
)

echo Replacement complete.
大大您好
其實我問過chatgpt很多次,但總是無法得到我要的結果
字串取代其實不難~但我要取代的是3行文字
也就是您上面這段程式中set "search=predefined_word"
這個predefined_word我不知道該怎麼把下面3行文字放進去
,
"HK_BTWN_FLT": [],
"LL_CATCHES": []
carloszhang wrote:
notepad++ ...(恕刪)
前面CONA24大大提到的notepad++在多個檔案中取代其實已經很好用了
完全不需要開啟檔案,只要選擇資料夾,就可以一次全部取代資料夾中所有檔案,連開啟檔案跟儲存檔案的動作都省下來了~~
算是目前手動方式中最簡單快速的~~
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?