Find and Replace - Speed up?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Find and Replace - Speed up?

#1 Post by SIMMS7400 » 12 Jan 2017 15:01

Hi Folks -

I have a file I source from an SAP system, that I need to massage, and then import to a target system.

One of the steps requires me to change the header.

I need to change:

Code: Select all

,Source,Short_Code,Desc,Code+Desc,Comp_code,Entity,Local_Currency,Management_Unit,Location,Country,Organizational_Parent,Functional_Parent

to:

Element,Source,Short_Code,Desc,Code+Desc,Comp_code,Entity,Local_Currency,Management_Unit,Location,Country,Organizational_Parent,Functional_Parent


Essentially, I'm just adding a name for the first column.

The code I'm using to perform the find and replace is as follows:

Code: Select all

    setlocal enableextensions disabledelayedexpansion
   
    set SEARCH=,Source,Short_Code,Desc,Code+Desc,Comp_code,Entity,Local_Currency,Management_Unit,Location,Country,Organizational_Parent,Functional_Parent
    set REPLACE=Element,Source,Short_Code,Desc,Code+Desc,Comp_code,Entity,Local_Currency,Management_Unit,Location,Country,Organizational_Parent,Functional_Parent
   set FILE_PATH=%LOCALEXPORTPATH%

   set "FN=%~n0.csv"
    set "FILE=%FILE_PATH%%FN%"
   
    for /f "delims=" %%i in ('type "%FILE%" ^& break ^> "%FILE%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        set "line=!line:%SEARCH%=%REPLACE%!"
        >>"%FILE%" echo !line!
        endlocal
    )


However, it takes forever since it cycles through the whole file. Is there a way to speed this up or somehow force it only look at line 1?

Thanks!

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

Re: Find and Replace - Speed up?

#2 Post by aGerman » 12 Jan 2017 16:39

Essentially, I'm just adding a name for the first column.

In this case you should probably just do what you essentially wanted to do :wink:
untested:

Code: Select all

...
<nul >"%FILE%~" set /p "=Element"
>>"%FILE%~" type "%FILE%"
>nul move /y "%FILE%~" "%FILE%"


Steffen

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Find and Replace - Speed up?

#3 Post by SIMMS7400 » 19 Jan 2017 15:42

Wow, thank you Steffen! This works much better!

:D

Post Reply