0

I have 5000 same files and I need to update numeric value in its content and increment it. Below is the batch script I use to find and replace a number in a certain file called BULK_1.txt.

I am not sure on how to increment the value after running search and replace.

@echo off 
setlocal EnableExtensions DisableDelayedExpansion

set search=01118596270001
set replace=01118596270002

set "textFile=BULK_1.txt"
set "rootDir=C:\Batch"

for %%j in ("%rootDir%\%textFile%") do (
    for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
        set "line=%%i"
        setlocal EnableDelayedExpansion
        set "line=!line:%search%=%replace%!"
        >>"%%~j" echo(!line!
        endlocal
    )
)
endlocal

The result should be like below. The last 4 digits should be updated from 0001 to 5000 for each file

Content of the BULK_1.txt:

DMAIN            Test_data        01118596270001
DDOC_DATA        Test_docdata     01118596270001

Content of the BULK_2:

DMAIN            Test_data        01118596270002
DDOC_DATA        Test_docdata     01118596270002

Content of the BULK_3:

DMAIN            Test_data        01118596270003
DDOC_DATA        Test_docdata     01118596270003
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 1
    increment would be `set /a cnt+=1` where the `cnt` variable would need to increase your initial value. see `set /?` from cmdline for help. but it does not seem as if you want to increment, it seems you want to use a value from the filename in the value itself, can you clarify? Will the last value always be the same as the end numeric value of the file name? – Gerhard Jan 10 '19 at 09:24
  • I'd reorder the elements in your question, situation as is, desired state and then your try to solve and what didn't work as expected. See [ask] –  Jan 10 '19 at 10:43
  • And time to accept an answer I think... – double-beep Feb 11 '19 at 11:46

2 Answers2

0

According to your requirements you may need:

@echo off
setlocal EnableDelayedExpansion

for /L %%A IN (1 1 5000) do (
    set num_processed=%%A
    call:find_len num_processed
    if !len! EQU 1 (set num_%%A=0111859627000%%A)
    if !len! EQU 2 (set num_%%A=011185962700%%A)
    if !len! EQU 3 (set num_%%A=01118596270%%A)
    if !len! EQU 4 (set num_%%A=0111859627%%A)
)

for /L %%A IN (1 1 5000) do (
    for /L %%B IN (1 1 2) do (
        if %%B EQU 1 (
            echo DMAIN            Test_data        !num_%%A!>BULK_%%A.txt
        ) else (
            echo DDOC_DATA        Test_docdata     !num_%%A!>>BULK_%%A.txt
        )
    )
)

:find_len
set "s=!%~1!#"
set "len=0"
for %%P in (4 2 1) do (
    if "!s:~%%P,1!" NEQ "" ( 
        set /a "len+=%%P"
        set "s=!s:~%%P!"
    )
)

My code uses the way that @jeb has suggested here slightly edited.

  • First, we make a loop to count from 1 to 5000.
    • We want to count the length of each of these numbers. calling the find_len subroutine does this.
    • If the string length of the variable is 1, then it must be 0001. The number in front is the same in all cases.
    • If the string length of the variable is 2, then it must be 0010. The number in front is the same in all cases.
    • If the string length of the variable is 3, then it must be 0100. The number in front is the same in all cases.
    • If the string length of the variable is 4, then it must be 1000. The number in front is the same in all cases.

Note: If we tried something similar to set /a 0000+1 the result would be 1. That is why all of these complicates!.

In all cases, the variable names will be num_numberCurrentlyProcessed.

  • Now another loop, actually the same as before. It will loop 5000 times and will create 5000 files in the format BULK_num.txt.
    • Inside this loop, another loop is required from 1 to 2, as each file must have 2 lines.
    • If we are in line 1, we echo the specific text specified by OP.
    • If we are in line 1, we echo again the specific text specified by OP.
double-beep
  • 5,031
  • 17
  • 33
  • 41
0

"I have 5000 same files" - it's a lot faster to write them from scratch than to edit each of them:

@echo off
setlocal enabledelayedexpansion

set "Hdr=DMAIN            Test_data        0111859627"
set "Dta=DDOC_DATA        Test_docdata     0111859627"

for /l %%a in (1,1,5000) do (
  set "num=0000%%a"     REM prepend some zeros
  set "num=!num:~-4!"   REM take last four chars
  >Bulk_%%a.txt (
    echo %Hdr%!num!
    echo %Dta%!num!
  )
)
Stephan
  • 53,940
  • 10
  • 58
  • 91