0

If I'm doing more text files, it will take too much time for this script because every text file have more than 500 lines.

This is the sample of input file:-

[MHMEM:"ID-01 (COLUMN-C10)"]
[LDDAT:0000000," ","01","T10",1,11,11,1150,"15N",600,420,130,600,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","02","T10",1,36,36,2600,"99165",420,820,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","03","T10",1,36,36,2100,"99165",170,820,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","04","T10",1,72,72,1375,"99165",200,420,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","05","T20",1,16,16,4425,"26",1000,245,3160,40,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]

I want output like this:-

CALL :Find "15N" "51"
CALL :Find "99165" "998"
CALL :Find "26" "26k"
[MHMEM:"ID-01 (COLUMN-C10)"]
[LDDAT:0000000," ","01","T10",1,11,11,1150,"51",600,420,130,600,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","02","T10",1,36,36,2600,"998",420,820,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","03","T10",1,36,36,2100,"998",170,820,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","04","T10",1,72,72,1375,"998",200,420,130,0,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
[LDDAT:0000000," ","05","T20",1,16,16,4425,"26k",1000,245,3160,40,0,0,0,0,0,0,0, , ,0,0,0, , ,0,0,0]
:Find <input1> <input2> <Notes>
@echo off & setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
    if exist "%%~na.tmp" del "%%~na.tmp"
    set "inputfile=%%~nxsa"
    for /f "delims=" %%b in (!inputfile!) do (
        for /f "tokens=1-30,* delims=," %%c in ("%%b") do (
            set "text=%~2"
            set "Note=%~3"
            if "%%k"=="" (
                echo %%b>>"%%~na.tmp"
            ) else (
                if not "%%k"==""%~1"" (
                    echo %%b>>"%%~na.tmp"
                ) else (
                    if "%%k"==""%~1"" set "Code=!text!" & set "L=!Note!" (
                        echo %%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,"!Code!",%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,!L!,,0,0,0,,,0,0,0]>>"%%~na.tmp"
                    )
                )
            )
        )
        del "%%~nxa"
        ren "%%~na.tmp" "%%~nxa"        
    )       
    exit/b
    goto :eof

Please let me know what I am doing wrong.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pugazh
  • 1
  • 2
  • 1
    Please read what I said about redirection in [this answer](https://stackoverflow.com/a/68146102/2152082) – Stephan May 18 '23 at 07:03
  • I have added indentation to highlight that your code has unbalanced parentheses, My assumption is that this is simply a typo, and the opening parenthesis on line 16, should be a closing parenthesis on line 17, (with the rest of the code adjusted accordingly). – Compo May 18 '23 at 08:44
  • @Stephan really i'm sorry, i can't understand about combine echo :( could you please expain it. – Pugazh May 18 '23 at 09:54
  • 1
    Simple: `for /L %%i in (1,1,1000) do echo %%i >> file.txt` searches the file, opens it, searches for the end of file, appends a line and closes the file again. One thousand times. `(for /L %%i in (1,1,1000) do echo %%i) > file.txt` opens the file once, writes a thousand lines at once and closes the file. Just one time. It's the finding, opening, reading and closing that takes the most time. The writing itself is rather quick. Put the whole `for` loop that writes to one file into an additional block `(`/`)` and redirect the whole block. – Stephan May 18 '23 at 12:06
  • Also, this code is superfluous and a waste of time: `for /f "delims=" %%b in (!inputfile!) do ( for /f "tokens=1-30,* delims=," %%c in ("%%b") do ( ...` Better use: `for /f "tokens=1-30,* delims=," %%c in (!inputfile!) do ( ...` – Aacini May 19 '23 at 03:46
  • @Aacini, `for /f "delims=" %%b in (!inputfile!) do ( for /f "tokens=1-30,* delims=," %%c in ("%%b") do ( ...` i'm using for this step echo for whole line without `"tokens=1-30,* delims=,"` – Pugazh May 19 '23 at 06:40

0 Answers0