在文件中的字串中插入換行符/特殊字符

在文件中的字串中插入換行符/特殊字符

我有 USER.TXT 檔案。文件包含文字:

123,234,987,877,356

我正在尋找腳本來將同一文件中的文字修改為:

123
234
987
877
356

請幫我編輯文字檔案而不將輸出重定向到新檔案。

到目前為止我有:

@echo off 
setlocal enableextensions disabledelayedexpansion

set "search=%,"
set "replace=%%"

set "textFile=USERS.txt"

for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    >>"%textFile%" echo(!line:%search%=%replace%!
    endlocal
)

感謝您感謝所有幫助!

答案1

將逗號替換為的批次腳本CRLF

如果這對您來說足夠批量,請嘗試一下,因為它看起來很簡單且有效。 。 。

下面的批次腳本本質上是:

筆記: set textFile=值應該是您將逗號變更為的文字檔案的完整明確路徑CRLF(例如C\Folder\Path\USERS.txt),或者如果此腳本與該檔案位於完全相同的資料夾中,則 的值應該以(例如)set textFile=為前綴%~dp0%~dp0USERS.txt

@echo on

set search=,
set textFile=C:\Folder\Path\USERS.txt
::set textFile=%~dp0USERS.txt

:PowerShell
SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%textFile%").replace("%search%", "`r`n") ^| Set-Content "%textFile%">"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
EXIT

原始文件內容

123,234,987,877,356

結果文件內容

123
234
987
877
356

更多資源

相關內容