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
try to add one string just after another string with comma
Moderator: DosItHelp
Re: try to add one string just after another string with comma
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.
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.
Re: try to add one string just after another string with comma
Thanks compo
Tried it out as below:
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%"
Re: try to add one string just after another string with comma
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!
Also, why are you thinking about the task in terms of search and replace? I see it as an insert task!
Re: try to add one string just after another string with comma
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
Re: try to add one string just after another string with comma
Based on the information you've provided, I think, (it's untested), this may be nearer to what you need:I've commented out three lines, because you didn't use %fk_sch%, but there's absolutely no reason why they cannot be uncommented
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%"