gpt4 book ai didi

arrays - 批处理 (CMD) 迭代两个数组(由 2 个 .txt 文件组成)

转载 作者:行者123 更新时间:2023-12-04 08:19:32 25 4
gpt4 key购买 nike

我想制作一个脚本,给出 2 个 .txt 文件,它创建 N 个文件,命名为第一个 .txt 文件中的值,并在该文件中插入第二个 .txt 文件中的值。

[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")
为了获取 .txt 文件中的值,我使用:
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
(我知道每个文件中的元素数量(它们是相同的))
你会怎么做?我尝试了很多变体,但都没有成功,例如:
for /F %%a in (file_2.txt) do (
for /l %%v in (1, 1, 92) do (
echo %%~nxa
)>> %%~nxv.html
)

最佳答案

...
设置数组{!s!}=%%a
...
创建 array{*}而不是 array[*]然后

for /L %%v in (1,1,%s%) do >"!array[%%v]!.html" echo !array{%%v}!
当然,您也可以将数组称为 names 之类的奇怪东西。和 texts例如并使用相同类型的括号,但我很想使用 name:%%itext:%%s完全避开括号。 ( : 因为它不能存在于文件名中) name_%%itext_%%s完全避开括号。 ( : 无法正常工作,改为 _ 有效;其他不太可能开始的字符,如 ] 无疑也可以工作)
----【实际测试代码】
@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
[结果]
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
[出于命名目的更改了代码]
@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
[相同的结果]

关于arrays - 批处理 (CMD) 迭代两个数组(由 2 个 .txt 文件组成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65556186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com