try to add one string just after another string with comma

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

try to add one string just after another string with comma

#1 Post by goodywp » 08 May 2018 09:24

I have a txt file, within this txt file, I have several lines, looks like below
xxxxxx <T501-08815-0103, 0501-08783-0100>
xxxxxx <T501-08815-0103>

Now I would like to add one string: T501-08700-0100 just after sting T501-08815-0103 with a comma to separate them, so after adding, it should looks like:

xxxxxx <T501-08815-0103,T501-08700-0100,0501-08783-0100>
xxxxxx <T501-08815-0103,T501-08700-0100>

Any thoughts?

Thanks
goodywp

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: try to add one string just after another string with comma

#2 Post by Compo » 08 May 2018 10:36

Please try a little harder! You have been given many answers and plenty of help here, and I'd wager sufficient for you to have written some code yourself in attempting the task.

Remember it's your task, we don't have to help; when we do it's expected that you don't just take that help, but use it to further your knowledge.

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: try to add one string just after another string with comma

#3 Post by goodywp » 08 May 2018 13:46

Thanks compo
Tried it out as below:

Code: Select all

@ECHO OFF
set gen_sch=T501-08815-0103
set sl_sch=T501-08700-0100
set fk_sch=0501-08783-0100

Set "search=%gen_sch%"
Set "replace=%gen_sch%,%sl_sch%"
Set "textfile=Readme-how-to-use-this-package.txt"
Set "newfile=indesnew.txt"
(
	For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
		If "%%B"=="" (
			Echo=
		) Else (
			Set "line=%%B"
			SetLocal EnableDelayedExpansion
			Set "line=!line:%search%=%replace%!"
			Echo=!line!
			EndLocal
		)
	)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: try to add one string just after another string with comma

#4 Post by Compo » 09 May 2018 07:01

You didn't provide any information with your code above, is it working as you wished, or are there one or more issues with it?

Also, why are you thinking about the task in terms of search and replace? I see it as an insert task!

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: try to add one string just after another string with comma

#5 Post by goodywp » 10 May 2018 07:52

Compo wrote:
09 May 2018 07:01
You didn't provide any information with your code above, is it working as you wished, or are there one or more issues with it?

Also, why are you thinking about the task in terms of search and replace? I see it as an insert task!
The info provided in the first post. The original task was to re-set the ReadMe file and can be divided into two sub-tasks
1) When the new profilename is DL and old profilename is SL then need to add scheme pack into ReadMe file such as
xxxxxx <T501-08815-0103, 0501-08783-0100>
xxxxxx <T501-08815-0103>
I tried to add T501-08700-0100 so it looks like this in the new ReadMe file
xxxxxx <T501-08815-0103,T501-08700-0100,0501-08783-0100>
xxxxxx <T501-08815-0103,T501-08700-0100>

2) When the new profilename is SL and old profilename is DL then need to remove scheme pack from Read file such as
xxxxxx <T501-08815-0103,T501-08700-0100,0501-08783-0100>
xxxxxx <T501-08815-0103,T501-08700-0100>
I tried to remove T501-08700-0100 so it looks like this in the updated ReadMe file
xxxxxx <T501-08815-0103, 0501-08783-0100>
xxxxxx <T501-08815-0103>

Here is the code and I got exactly what I want even in format no difference...

Code: Select all

@ECHO OFF

set profname=MOCKUP_DL
set oprofname=QA_SL
set profname=%profname:~-2%
set oprofname=%oprofname:~-2%

if "%profname%"=="DL" (

	if "%oprofname%"=="DL" goto end
 
	if "%oprofname%"=="SL" goto DL

	) else (

	if "%oprofname%"=="DL" goto SL
 
	if "%oprofname%"=="SL" goto end

	)

:: here to remove SL scheme pack from ReadMe file
:DL
cd C:\Users\abc

set gen_sch=T501-08815-0103
set sl_sch=T501-08700-0100
set fk_sch=0501-08783-0100

@echo off 
Set "search=%sl_sch%,"
Set "replace="
Set "textfile=Readme-how-to-use-this-package.txt"
Set "newfile=indesnew.txt"

(
	For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
		If "%%B"=="" (
			Echo=
		) Else (
			Set "line=%%B"
			SetLocal EnableDelayedExpansion
			Set "line=!line:%search%=%replace%!"
			Echo=!line!
			EndLocal
		)
	)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"

timeout /t 1

Set "search=,%sl_sch%"
Set "replace="
Set "textfile=Readme-how-to-use-this-package.txt"
Set "newfile=indesnew.txt"

(
	For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
		If "%%B"=="" (
			Echo=
		) Else (
			Set "line=%%B"
			SetLocal EnableDelayedExpansion
			Set "line=!line:%search%=%replace%!"
			Echo=!line!
			EndLocal
		)
	)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"

goto end

:: here to add SL scheme pack into ReadMe file
:SL

Set "search=%gen_sch%"
Set "replace=%gen_sch%,%sl_sch%"
Set "textfile=Readme-how-to-use-this-package.txt"
Set "newfile=indesnew.txt"
echo 66
(
	For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
		If "%%B"=="" (
			Echo=
		) Else (
			Set "line=%%B"
			SetLocal EnableDelayedExpansion
			Set "line=!line:%search%=%replace%!"
			Echo=!line!
			EndLocal
		)
	)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"

:end

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: try to add one string just after another string with comma

#6 Post by Compo » 10 May 2018 20:57

Based on the information you've provided, I think, (it's untested), this may be nearer to what you need:

Code: Select all

@Echo Off
Set "profname=MOCKUP_DL"
Set "oprofname=QA_SL"

If /I Not "%profname:~-2%%oprofname:~-2%"=="DLSL" (
	If /I Not "%profname:~-2%%oprofname:~-2%"=="SLDL" GoTo :EOF)

Set "gen_sch=T501-08815-0103"
Set "sl_sch=T501-08700-0100"
::Set "fk_sch=0501-08783-0100"
Set "textfile=Readme-how-to-use-this-package.txt"
Set "srcdir=C:\Users\abc"

If /I "%gen_sch%"=="%sl_sch%" GoTo :EOF
::If /I "%fk_sch%"=="%gen_sch%" GoTo :EOF
::If /I "%fk_sch%"=="%sl_sch%" GoTo :EOF
If Not Exist "%srcdir%\%textfile%" GoTo :EOF
Find /I "<%gen_sch%"<"%srcdir%\%textfile%">Nul || GoTo :EOF

(For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%srcdir%\%textfile%"'
	) Do If "%%B"=="" (Echo=) Else (SetLocal EnableDelayedExpansion
		Set "line=%%B"
		If "%profname:~-2%"=="DL" (Set "line=!line:,%sl_sch%,=,!"
			Set "line=!line:,%sl_sch%>=>!") Else (
			Set "line=!line:%gen_sch%=%gen_sch%,%sl_sch%!")
		Echo=!line!
		EndLocal))>"%srcdir%\%newfile%"
Del "%srcdir%\%textfile%"
Ren "%srcdir%\%newfile%" "%textfile%"
I've commented out three lines, because you didn't use %fk_sch%, but there's absolutely no reason why they cannot be uncommented

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: try to add one string just after another string with comma

#7 Post by goodywp » 11 May 2018 12:27

Thanks a lot!

Post Reply