shivam
shivam

Reputation: 383

Add string in csv file via windows batchscript

I have a pipe delimited csv file like below

METADATA|WorkTerms|PeriodOfServiceId
MERGER|WorkTerms|WT10127
METADATA|Assignment|WorkTermsAssignmentId|PeriodOfServiceId
MERGER|Assignment|WT10127|WR10127

I want a output like below

METADATA|WorkTerms|PeriodOfServiceId(SourceSystemId)
MERGER|WorkTerms|WT10127
METADATA|Assignment|WorkTermsAssignmentId(SourceSystemId)|PeriodOfServiceId(SourceSystemId)
MERGER|Assignment|WT10127|WR10127

I want to add (SourceSystemId) at specific places, can this be done via windows Batch script ?

Any leads, links suggestions would be really helpful.

Thanks,

Shivam

Upvotes: 0

Views: 191

Answers (1)

user7818749
user7818749

Reputation:

Firstly, replace the name of myfile.csv in the 4th line only, with the name of your csv file and te path to the file in the 3rd line.. If the strings you want to replace are the only strings, do not touch anything else in the script. place the script in the same directory as your files:

@echo off
setlocal enabledelayedexpansion
set "mypath=C:\PATH\TO\CSV"
set "mycsv=myfile.csv"
move "%mypath%\%mycsv%" "%mypath%\oldmyfile.txt"

for /f "delims=" %%i in ('type "%mypath%\oldfile.txt"') do (
    set "str=%%i"
    set "edit=!str:PeriodOfServiceId=PeriodOfServiceId(SourceSystemId)!"
    set "edit=!edit:WorkTermsAssignmentId=WorkTermsAssignmentId(SourceSystemId)!"
    echo !edit! >> "%mypath%\%mycsv%"
)
del "%mypath%\oldfile.txt" /Q

Here is what the script does. Renames the original file to oldfile.txt, type each line and make it a variable, do a replacement of certain fields, but the fields that does not match the search string will remain in tact, then it will echo the lines to the original file name including the replacements.

Upvotes: 1

Related Questions