I'm trying to remove extra comma from end of the line in text file. Below is my approach:
set "n=%~,"
for %%t in (*.txt) do (
findstr /v /r /c:"$[%n%]*$" > res.txt
)
But it's no replacing the extra comma from text file. The content of text file as follows:
abc,asd,123,
1234,prq,456,,,,,
jkl,abc,9876,,,
5679,3459,gjh,,
I want the expected output as follows:
abc,asd,123
123,prq,456
jkl,abc,9878
5679,3459,gjh
I think substring will help you to get it done:
@echo off
setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
set txtPath=%%~fa
echo !txtPath!
for /f %%b in ('type "!txtPath!"') do (
set line=%%b
set output=!line!
if "!line:~-1!"=="," (
for /l %%i in (1,1,1000) do if "!output:~-1!"=="," set output=!output:~0,-1!
)
echo !output!>> res.txt
)
)
As shown in the code above, It will help to trim up to 1,000 unwanted trailing comma and place the formatted output to a text file. You may refer to this link for more information about string manipulation in batch. Hope it helps.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments