Replacing "" by " in every line of a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
afastboi
Posts: 3
Joined: 23 Oct 2020 04:21

Replacing "" by " in every line of a text file

#1 Post by afastboi » 23 Oct 2020 04:30

Hello !

I have this problem :

I have a text file with lines that look like this :
"300040082800016351540;01;EUR;310719; 84,67-;"
"300040082800016351540;07;EUR;010819; 84,67-;"
"300040082800016356293;01;EUR;310719; 261,83+;"
"300040082800016356293;07;EUR;010819; 261,83+;"
"300040082800016356390;01;EUR;310719; 183,16+;"
"300040082800016356390;07;EUR;010819; 183,16+;"
"130880909300350900060;01;EUR;300719; 20127893,81+;"
130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; "29,64+;"
130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; "39,36+;"
130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; "58,94+;"
130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; "68,88+;"
130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; "296,34+;"
130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; "321,21+;"
as you can see, some " are missing on the first line

I cam up with a program to add " to every line :

Code: Select all

setLocal EnableDelayedExpansion

for /f "delims=" %%a in ('type *.txt') do (
	set test="%%a
	echo !test! >> fichier_texte_traitee.txt
)
So it gives :
""300040082800016351540;01;EUR;310719; 84,67-;"
""300040082800016351540;07;EUR;010819; 84,67-;"
""300040082800016356293;01;EUR;310719; 261,83+;"
""300040082800016356293;07;EUR;010819; 261,83+;"
""300040082800016356390;01;EUR;310719; 183,16+;"
""300040082800016356390;07;EUR;010819; 183,16+;"
""130880909300350900060;01;EUR;300719; 20127893,81+;"
"130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; "29,64+;"
"130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; "39,36+;"
"130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; "58,94+;"
"130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; "68,88+;"
"130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; "296,34+;"
"130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; "321,21+;"
So now, i want to replace the "" with ", but i've been searching and trying for hours, i can't find a way to do this...

Any help would be very appreciated :D

Thank you in advance.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Replacing "" by " in every line of a text file

#2 Post by miskox » 23 Oct 2020 06:15

This seems to work:

Code: Select all

@echo off
set var=""test"

set new_var=%var:""="%

echo _%var%_
echo _%new_var%_
Result:

Code: Select all

_""test"_
_"test"_
Saso

afastboi
Posts: 3
Joined: 23 Oct 2020 04:21

Re: Replacing "" by " in every line of a text file

#3 Post by afastboi » 23 Oct 2020 07:22

Hello !

Thanks for your answer, but it doesn't work for me

I tried to implement it like this :

Code: Select all

setLocal EnableDelayedExpansion

for /f "delims=" %%a in ('type *.txt') do (
	set var=%%a
	set iable=%var:""="%
	
	echo %iable%
)
when echoing "iable", it looks like it is empty...

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Replacing "" by " in every line of a text file

#4 Post by aGerman » 23 Oct 2020 10:16

You're tryin to update and expand variables inside of the body of the loop where variables are expanded only once before the loop is executed. If you use delayed variable expansion to work around this issue, then enclose variables into exclamation points rather than into percent signs.

Code: Select all

for /f "delims=" %%a in ('type *.txt') do (
	set var=%%a
	echo !var:""="!
)
Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replacing "" by " in every line of a text file

#5 Post by Squashman » 23 Oct 2020 13:45

Code: Select all

@echo off

(for /f "delims=" %%G in (input.txt) do (
	echo %%G|findstr /B ^""" ||echo "%%G
))>Output.txt

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Replacing "" by " in every line of a text file

#6 Post by dbenham » 23 Oct 2020 15:39

I am pretty sure your stated goal is not what you really want. Even if you manage to convert paired double quotes into a single double quote, the last lines will still have unbalanced quotes because the last field was already quoted.

It looks to me like your source file is a corrupted CSV using semicolon as a delimiter. Since none of your values contain any semicolons, you should be able to simply remove all quotes

Code: Select all

@echo off
setlocal enableDelayedExpansion
>output.csv (
  for /f "delims=" %%L in ('type *.txt 2^>nul') do (
    set "ln=%%L"
    echo(!ln:"=!
  )
)
--OUTPUT--

Code: Select all

300040082800016351540;01;EUR;310719; 84,67-;
300040082800016351540;07;EUR;010819; 84,67-;
300040082800016356293;01;EUR;310719; 261,83+;
300040082800016356293;07;EUR;010819; 261,83+;
300040082800016356390;01;EUR;310719; 183,16+;
300040082800016356390;07;EUR;010819; 183,16+;
130880909300350900060;01;EUR;300719; 20127893,81+;
130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; 29,64+;
130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; 39,36+;
130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; 58,94+;
130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; 68,88+;
130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; 296,34+;
130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; 321,21+;
Better yet, fix the code that is generating the corrupted data in the first place.

I suspect the trailing ; in my output is also not correct, but it could be. Also the leading or trailing space in some fields might be a problem, depending on what software is using the resultant CSV.

If I am wrong about CSV, then tell us what file format you are trying to create, and show us exactly how each line should look. Think hard about how the resultant file will be used.


Dave Benham

afastboi
Posts: 3
Joined: 23 Oct 2020 04:21

Re: Replacing "" by " in every line of a text file

#7 Post by afastboi » 26 Oct 2020 06:37

Wow, thank you for the answers everyone !

@dbenham i didn't think of that at all.
I am in apprenticeship, and is the only "tech guy" on my field.
The first output i gave with the " placed in a wrong position are given by a robot, but i can not access it / modify it.

The file is then used by a program to input the lines in a excel file. I will try using the lines without any " with the program, and keep you informed.

Thanks again :) !

Post Reply