Batch (CMD) Iterate over two arrays (made out of 2 .txt files)

Viewed 82

I would like to make a script that given 2 .txt files, it creates N files named as the values inside the first .txt file, and insert in that file the value inside the second .txt file.

[FILE_1].txt
name_1
name_2
name_3
name_4


[FILE_2].txt
text_1
text_2
text_3
text_4



Result:

name_1.html (with inside the string "text_1")
name_2.html (with inside the string "text_2")
name_3.html (with inside the string "text_3")
name_4.html (with inside the string "text_4")

In order to take the values inside the .txt file I use:

setlocal EnableDelayedExpansion

set i=0
for /F %%a in (file_1.txt) do (
   set /A i+=1
   set array[!i!]=%%a
)
set n=%i%



set s=0
for /F %%a in (file_2.txt) do (
   set /A s+=1
   set array[!s!]=%%a
   
)
set v=%s%


endlocal

(I know the number of elements in each file (they are the same)) How would you do it? I tried a lot of variations, with no success, like:

for /F %%a in (file_2.txt) do (
 for /l %%v in (1, 1, 92) do (
 echo %%~nxa 
  )>> %%~nxv.html
)
2 Answers

... set array{!s!}=%%a ...

To create array{*} instead of array[*]

Then

for /L %%v in (1,1,%s%) do >"!array[%%v]!.html" echo !array{%%v}!

Of course, you could also call the arrays something bizarre like names and texts for instance and use the same type of brackets, but I'd be tempted to use name:%%i and text:%%s to avoid the brackets completely. (: because it can't exist within a filename) name_%%i and text_%%s to avoid the brackets completely. (: didn't work properly, changed to _ that worked; Other unlikely-to-start-a-line characters like ] would no doubt also work)

---- [actual test code]

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65556186.txt"
SET "filename2=%sourcedir%\q65556186_2.txt"

set i=0
for /F "usebackq" %%a in ("%filename1%") do (
   set /A i+=1
   set array[!i!]=%%a
)
set n=%i%

set s=0
for /F "usebackq" %%a in ("%filename2%") do (
   set /A s+=1
   set array{!s!}=%%a
)
for /L %%v in (1,1,%s%) do >"%destdir%\!array[%%v]!.html" echo !array{%%v}!

TYPE "%destdir%\*.html"

GOTO :EOF

[results]

u:\your results\name_1.html


text_1

u:\your results\name_2.html


text_2

u:\your results\name_3.html


text_3

u:\your results\name_4.html


text_4

[with changes to code for naming purposes]

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65556186.txt"
SET "filename2=%sourcedir%\q65556186_2.txt"

set i=0
for /F "usebackq" %%a in ("%filename1%") do (
   set /A i+=1
   set names_!i!=%%a
)
set n=%i%

set s=0
for /F "usebackq" %%a in ("%filename2%") do (
   set /A s+=1
   set texts_!s!=%%a
)
for /L %%v in (1,1,%s%) do >"%destdir%\!names_%%v!.html" echo !texts_%%v!

TYPE "%destdir%\*.html"

GOTO :EOF

[Same results]

You could probably do it like this:

@SetLocal EnableExtensions EnableDelayedExpansion
@(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("[FILE_1].txt") Do 1> "%%G.html" (Set /P "}="&Echo(!}!)) 0< "[FILE_2].txt"
Related