파일의 문자열에 줄 바꿈/특수 문자 삽입

파일의 문자열에 줄 바꿈/특수 문자 삽입

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

추가 리소스

관련 정보