Insert new line at certain line works but the lines starting with ; all gone, why?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Insert new line at certain line works but the lines starting with ; all gone, why?

#1 Post by goodywp » 17 Oct 2019 13:55

Hi
I tried to insert a new line at a certain line. It works via below two code pieces but all the lines beginning with ; are all gone, why? any solution?


My original file (Load_SDK.M71) looks like this
  • ;
    ;----------WebPlatForm_2.10.9.B----------------------
    9993047039001700_WP_APPS_BROWSER.P3O
    9993047041001700_LINGOT_WP_LIB.P3O
    9993047043001700_LINGOT_WP_SYS_L.P3O
    9983047033002908_WP_WebAppDaem_M.P3A
    9983047034002908_WP_WebAppDaem_V.P3A
    9990047038021009_WP_AddOn.P3P
    9990047117002908_WP_libEss.P3L
    9993047035002908_WP_libWebApp.P3L
    9993047036002908_WP_libUtils.P3L
    9993047037002908_WP_libAbcProt.P3L
    9993047047002908_WP_WebOsDaemon.P3A
    9993047031002908_WP_WBService.P3P
    9993047031002908_WP_WBService.P3A
    ;
    ;
    -eimport
I used the below code it can insert line 9993047045001700_LINGOT_WP_MKP.P3O
after 9993047031002908_WP_WBService.P3P

Code: Select all

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

set inputFile=Load_SDK.M71
set outputFile=Load_SDKtest.M71
set _strInsert=9993047045001700_LINGOT_WP_MKP.P3O
set _strFind=9993047031002908_WP_WBService.P3P

FOR /F "usebackq delims=" %%A IN ("%inputFile%") DO (
  Echo %%A | Find "%_strFind%" && ECHO %%A>>"%outputFile%" && ECHO %_strInsert%>>"%outputFile%"
  IF [!errorlevel!] == [1] ECHO %%A>>"%outputFile%"
)
the out put file (Load_SDKtest.M71) looks like below
  • 9993047039001700_WP_APPS_BROWSER.P3O
    9993047041001700_LINGOT_WP_LIB.P3O
    9993047043001700_LINGOT_WP_SYS_L.P3O
    9983047033002908_WP_WebAppDaem_M.P3A
    9983047034002908_WP_WebAppDaem_V.P3A
    9990047038021009_WP_AddOn.P3P
    9990047117002908_WP_libEss.P3L
    9993047035002908_WP_libWebApp.P3L
    9993047036002908_WP_libUtils.P3L
    9993047037002908_WP_libAbcProt.P3L
    9993047047002908_WP_WebOsDaemon.P3A
    9993047031002908_WP_WBService.P3P
    9993047045001700_LINGOT_WP_MKP.P3O
    9993047031002908_WP_WBService.P3A

    -eimport
but all the lines with ; are gone....

I would like to keep these lines same as original, only insert line 9993047045001700_LINGOT_WP_MKP.P3O

Thanks

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: Insert new line at certain line works but the lines starting with ; all gone, why?

#2 Post by Eureka! » 17 Oct 2019 14:13

";" is the default EOL character in FOR commands.
Use a dummy character that is not in the lines you want to parse (like "@") : eol=@

See FOR /? for details.

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

Re: Insert new line at certain line works but the lines starting with ; all gone, why?

#3 Post by goodywp » 17 Oct 2019 14:45

Thanks Eureka's help!

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

set inputFile=Load_SDK.M71
set outputFile=Load_SDKtest.M71
set _strInsert=9993047045001700_LINGOT_WP_MKP.P3O
set _strFind=9993047031002908_WP_WBService.P3P

FOR /F "usebackq delims= eol=@" %%A IN ("%inputFile%") DO (
  Echo %%A | Find "%_strFind%" && ECHO %%A>>"%outputFile%" && ECHO %_strInsert%>>"%outputFile%"
  IF [!errorlevel!] == [1] ECHO %%A>>"%outputFile%"
)
It works as expected... :D


Post Reply