Replacement of Operator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Replacement of Operator

#1 Post by pp8771 » 18 Jul 2018 04:18

The file a1.dat contains

Code: Select all

COMPID~COMPNAME~ADDRESS~YEAROFESTABLISTMENT
I have written a code to replace the operator ~ by "," and append " in the beginning and end of the line.
So written a small code.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set vffname=D:\a1.dat
REM echo !vffname!
set "vtilda=~"
REM remember       vtilda can be % ,
  

  set vheader=
  set /p "vheader="<"!vffname!"
    echo 1 !vheader!
	set vheader="!vheader!"
	echo 2 !vheader!
  REM set vheader="!vheader:|=","!"
    set   vheader="!vheader:!!vtilda!!=","!"
  echo  3 !vheader!
Required Output

Code: Select all

"COMPID","COMPNAME","ADDRESS","YEAROFESTABLISTMENT"
Getting an strange error:

Code: Select all

1 COMPID~COMPNAME~ADDRESS~YEAROFESTABLISTMENT
2 "COMPID~COMPNAME~ADDRESS~YEAROFESTABLISTMENT"
 3 "~"

pieh-ejdsch
Posts: 204
Joined: 04 Mar 2014 11:14
Location: germany

Re: Replacement of Operator

#2 Post by pieh-ejdsch » 18 Jul 2018 11:56

Hello,
You only make this special replacement by

Code: Select all

:replace
for /f "tokens=1*delims=~" %%i in ("!vheader!") do (
  if .%%j neq . (
    set "vheader=%%i","%%j"
    goto :replace
  )
)
echo !vheader! 
Phil

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Replacement of Operator

#3 Post by pp8771 » 18 Jul 2018 13:18

Delimiter will be a parameter and not an hard code value
I want to put that in a variable set "vtilda=~"

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

Re: Replacement of Operator

#4 Post by Squashman » 18 Jul 2018 13:37

pp8771 wrote:
18 Jul 2018 13:18
Delimiter will be a parameter and not an hard code value
I want to put that in a variable set "vtilda=~"
Then why did you name the variable tilda if it is really delimiter?

And what happened when you changed the code so that the delims= option had your variable name after it?

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Replacement of Operator

#5 Post by pp8771 » 18 Jul 2018 14:37

Now modified it for a single line

Code: Select all

@echo off
setlocal enabledelayedexpansion
set vffname=D:\a1.dat
REM echo !vffname!
set "vtilda=^="
 REM echo    !vtilda!   
  
   REM echo: FILE SIZE !vsize!
  set vheader=
  REM set vcolumnlist=
  set /p "vheader="<"!vffname!"
  setlocal
    :replace
for /f "tokens=1*delims=%vtilda%" %%i in ("!vheader!") do (
  if .%%j neq . (
    set "vheader=%%i","%%j"
    goto :replace
  )
)
set vheader="!vheader!"
echo !vheader!
endlocal
	
	
	REM echo 1 !vheader!
	REM set vheader="!vheader!"
	REM echo 2 !vheader!
  REM set vheader="!vheader:|=","!"
  REM set   vheader=!vheader:~=","!
  REM echo  3 !vheader!
  pause

But not able to read multiple lines

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

Re: Replacement of Operator

#6 Post by Squashman » 18 Jul 2018 14:45

pp8771 wrote:
18 Jul 2018 14:37
But not able to read multiple lines
You never specified that you needed to do that in your original requirements. If you need to read the entire file then use another FOR /F command to read the file. Then use a CALL to the function that changes the delimiter. But personally I would just use Dave's ParseCSV.bat

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Replacement of Operator

#7 Post by pp8771 » 19 Jul 2018 01:30

My input file [a1.dat] is

Code: Select all

COMPID=COMPNAME=ADDRESS=YEAROFESTABLISTMENT
100=XYC=AWER RD=12072018
120=BNM=PQTY RD=12082018
100=XYC=AWER RD=12072018
120=BNM=PQTY RD=12082018
Required output

Code: Select all

'100','XYC','AWER RD','12072018'
'120','BNM','PQTY RD','12082018'
'100','XYC','AWER RD','12072018'
'120','BNM','PQTY RD','12082018'

But this code is not working. Assistance is appreciated.it is going in infinite loop.now some it is working.

Code: Select all

@echo off
REM setlocal enabledelayedexpansion

GOTO :MAIN	

setlocal
:replace

echo 2.1 %~1 %~2
set vdl=%~2
set vln=%~1
:replace2
for /F "tokens=1*delims=%vdl%" %%q in ("%vln%") do (
echo 2.2  %%q %%r
 pause
  if .%%r neq . (
    set "vln=%%q','%%r"
	echo 2.3 !vln!
	
     goto :replace2
  )
   
)

echo 2.4 !vln!
set vln='!vln!'
echo 2.5 !vln!
set "%~3=%vln%"
goto :EOF
	
:MAIN	
set vffname=D:\a1.dat
REM echo !vffname!
set "vtilda=^="
 REM echo    !vtilda!   
  
   REM echo: FILE SIZE !vsize!
	REM echo 1 !vheader!
	REM set vheader="!vheader!"
	REM echo 2 !vheader!
  REM set vheader="!vheader:|=","!"
  REM set   vheader=!vheader:~=","!
  REM echo  3 !vheader!
  
   FOR /F "skip=1 usebackq delims=" %%G IN ("!vffname!") DO (
        
    	set "line=%%~G"	
		
    	REM set "line='!line:|=','!'"
		REM echo !line!
		
		setlocal
		call :replace "!line!"  "%vtilda%" fline
        
		echo 1.2 !fline!
   		set iline=!fline!
		 endlocal
		
		)
  pause


But when I am reading the header of the file now things are working fine.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set vffname=D:\a1.dat
REM echo !vffname!
set "vtilda=^="
 REM echo    !vtilda!   
  
   REM echo: FILE SIZE !vsize!
  set vheader=
  REM set vcolumnlist=
  set /p "vheader="<"!vffname!"
  setlocal
    :replace
for /f "tokens=1*delims=%vtilda%" %%i in ("!vheader!") do (
  if .%%j neq . (
    set "vheader=%%i","%%j"
	echo !vheader!
    goto :replace
  )
)
set vheader="!vheader!"
echo !vheader!
endlocal
	
	
	REM echo 1 !vheader!
	REM set vheader="!vheader!"
	REM echo 2 !vheader!
  REM set vheader="!vheader:|=","!"
  REM set   vheader=!vheader:~=","!
  REM echo  3 !vheader!
  pause

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Replacement of Operator

#8 Post by pp8771 » 20 Jul 2018 05:03

Really I am looking for assistance from senior members how to read the delimited files.

The code is working for delimiter ~,|,=, # but not for % . can any one has idea what is wrong with this code.

Assistance is really appreciated.

My file a1.dat is:

Code: Select all

COMPID%COMPNAME%ADDRESS%YEAROFESTABLISTMENT
100%XYC%AWER RD%12072018
120%BNM%PQTY RD%12082018
100%XYC%AWER RD%12072018
120%BNM%PQTY RD%12082018

Code: Select all

@echo off
REM setlocal enabledelayedexpansion

GOTO :MAIN	

setlocal
:replace

echo 2.1 %~1 %~2
set vdl=%~2
set vln=%~1
echo 2.1.1 %vln%  delimite   %vdl%
:replace2
for /F "tokens=1*delims=%vdl%" %%q in ("%vln%") do (
echo 2.2  %%q %%r
  if .%%r neq . (
    set "vln=%%q','%%r"
	echo 2.3 !vln!
	
     goto :replace2
  )
   
)

echo 2.4 !vln!
set vln='!vln!'
echo 2.5 !vln!
set "%~3=%vln%"
goto :EOF
	
:MAIN	
set vffname=D:\a1.dat
REM echo !vffname!
set "vtilda=%%"
 REM echo    !vtilda!   
  
   REM echo: FILE SIZE !vsize!
	REM echo 1 !vheader!
	REM set vheader="!vheader!"
	REM echo 2 !vheader!
  REM set vheader="!vheader:|=","!"
  REM set   vheader=!vheader:~=","!
  REM echo  3 !vheader!
  
   FOR /F "skip=1 usebackq delims=" %%G IN ("!vffname!") DO (
        
    	set "line=%%~G"	
		
    	REM set "line='!line:|=','!'"
		REM echo !line!
		
		setlocal
		call :replace "!line!"  "!vtilda!" fline
        
		echo 1.2 !fline!
   		set iline=!fline!
		 endlocal
		
		)
  pause

Required output should be :

Code: Select all

'100','XYC','AWER RD','12072018'
'120','BNM','PQTY RD','12082018'
'100','XYC','AWER RD','12072018'
'120','BNM','PQTY RD','12082018'

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

Re: Replacement of Operator

#9 Post by Squashman » 20 Jul 2018 06:34

pp8771 wrote:
20 Jul 2018 05:03
but not for % .
Well that character should be a huge clue as to why it does not work. That character is obviously used in batch files for variable expansion so it is a special character. If you need to use a literal % character then you need to double it, whenever you need to use it as a literal to escape the variable expansion usage.

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: Replacement of Operator

#10 Post by pp8771 » 20 Jul 2018 06:44

By using %% it does not work. I have tried it. Any other suggestion.

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

Re: Replacement of Operator

#11 Post by Squashman » 22 Jul 2018 15:29

In the simplest form possible, if you want to change the delimiter of your file all you have to do is this.

Code: Select all

@echo off

set "vffname=input.txt"
SET "delim=%%"

for /F "tokens=1-4 delims=%delim%" %%G in (%vffname%) do echo '%%~G','%%~H','%%~I','%%~J'
pause

Post Reply