Shalapolia
Shalapolia

Reputation: 146

Batch script to process multiple .txt files processes only one

I have a batch script that should process multiple input files, and create a separate output file for each. However, my issues are:

  1. It reads only the first text file
  2. It goes into an infinite loop after creating the first output file.

I need the script to make a separate output file for each input file. How can I achieve this?

The code is currently as follows:

@echo off
setlocal enabledelayedexpansion
set line=0
for /r %%x in (*.txt) do (                     //SUPPOSED to read all input files
for /f "tokens=4 delims=|" %%a in (%%x) do (... goto: GETLINE))

:GETLINE
if not %line%==0 set skip=skip=%line%
for %%x in (*.txt) do ( ...
echo %%b >>"Output_%%x.txt"                           //writing into output
goto :BREAK
))
:BREAK

Upvotes: 0

Views: 1709

Answers (1)

Bali C
Bali C

Reputation: 31221

I think the issue is in the :GETLINE function.

If that is called you are looping over all files again, and reading each line again.

Try using this instead

@echo off
setlocal enabledelayedexpansion
set line=0
for /r %%x in (*.txt) do (
for /f "tokens=4 delims=|" %%a in (%%x) do (
set num=%%a
set num=!num: =!
if !num!==589 set bool=true
if !num!==581 set bool=true
if !num!==580 set bool=true
if !num!==027 set bool=true
if !num!==582 set bool=true
if !num!==585 set bool=true
if "!bool!"=="true" call :GETLINE "%%x"
set /a line+=1
set bool=false
)
)

:GETLINE
if not %line%==0 set skip=skip=%line%
for /f "usebackq %skip% tokens=* delims=" %%b in ("%~1") do (
echo %%b >>"Output_%%x.txt"
goto :BREAK
)
)
:BREAK

Which will call :GETLINE and pass it the current file in the loop, which will stop it from looping everything again.

Upvotes: 1

Related Questions