1

I have USER.TXT file. File contain text:

123,234,987,877,356

I looking for script to modify text in the same file to:

123
234
987
877
356

Please, help me out for editing a text file without redirecting the output to a new file.

So Far I Have:

@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
)

Thank you appreciate all help!!!

2
  • Did not work. Here is the Output I'm getting: Jun 11 '18 at 11:11
  • See Accepting an Answer to ensure you understand how that works for the solution you find for any answers that help you resolve your problem by simply checking the little gray check box to the upper left side of the answer that resolves your problem assuming one does and turn it green to show it's the answer you accepted since that's how that is supposed to work; you ask a question, you get answers, and if you get an answer that works for your needs, you simply accept that answer. Jun 20 '18 at 20:25
1

Batch Script to Replace Comma with CRLF

If this is batch enough for you, give it a shot as it seems to be simple and just works . . .

The below batch script will essentially:

  • Use Get-Content and Replace for the string to search (,) and replace (CRLF)
  • Then it will use Set-Content to put the newly replaced string back into the file accordingly

Note: The set textFile= value should be either the full explicit path to the text file you'll change commas to CRLF (e.g. C\Folder\Path\USERS.txt), or else if this script is in the exact same folder as that file, the value of the set textFile= should be prefixed with %~dp0 (e.g. %~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

Original File Content

123,234,987,877,356

Result File Content

123
234
987
877
356

Further Resources

4
  • Did not work . Getting error on output:Get-Content : Cannot find path 'C:\Windows\System32\WindowsPowerShell\v1.0\Users.txt' because it does not exist Jun 11 '18 at 11:16
  • @GeorgeDudnikov I just edited my answer with a correction to resolve that. I used the full explicit path in my testing but I added a Note section with two solutions that should work for you depending on your setup. Jun 11 '18 at 12:41
  • Thank you very Much!! Excellent Resolution!!!. you are the Best! Jun 11 '18 at 14:22
  • @GeorgeDudnikov See Accepting an Answer to ensure you understand how that works for the solution you find for any answers that help you resolve your problem by simply checking the little gray check box to the upper left side of the answer that resolves your problem assuming one does and turn it green to show it's the answer you accepted since that's how that is supposed to work; you ask a question, you get answers, and if you get an answer that works for your needs, you simply accept that answer. Jun 20 '18 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.