Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 批处理(CMD)迭代两个数组(由2.txt文件组成)_Arrays_Loops_For Loop_Batch File_Cmd - Fatal编程技术网

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

Arrays 批处理(CMD)迭代两个数组(由2.txt文件组成),arrays,loops,for-loop,batch-file,cmd,Arrays,Loops,For Loop,Batch File,Cmd,我想制作一个脚本,给定2.txt文件,它会在第一个.txt文件中创建名为值的N个文件,并在该文件中插入第二个.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 strin

我想制作一个脚本,给定2.txt文件,它会在第一个.txt文件中创建名为值的N个文件,并在该文件中插入第二个.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[*]

然后

当然,你也可以把数组称为奇怪的东西,比如
名称
文本
,并使用相同类型的括号,但我会尝试使用
名称:%%I
文本:%%s
来完全避免括号。(
,因为它不能存在于文件名中)
name\%%i
text\%%s
以完全避免括号。(
工作不正常,改为
工作正常;其他不太可能以行开头的字符,如
]
无疑也可以工作)

----[实际测试代码]

@ECHO关闭
SETLOCAL ENABLEDELAYEDEXPANSION
rem源目录、目标目录、目标目录的以下设置,
rem批处理目录、文件名、输出文件名和临时文件名[如果显示]是名称
我用于测试的rem,故意包含包含空格的名称,以确保
rem确认流程使用这些名称工作。这些将需要改变,以适应您的情况。
设置“sourcedir=u:\your files”
设置“destdir=u:\your results”
设置“filename1=%sourcedir%\q65556186.txt”
设置“filename2=%sourcedir%\q65556186_2.txt”
集合i=0
对于(“%filename1%”中的/F“usebackq”%%a,请执行以下操作(
设置/A i+=1
设置数组[!i!]=%%a
)
设置n=%i%
设置s=0
对于(“%filename2%”中的/F“usebackq”%%a,请执行以下操作(
设置/A s+=1
集合数组{!s!}=%%a
)
对于(1,1,%s%)中的/L%%v,请执行>%destdir%\!数组[%%v]!.html“echo!”!数组{%%v}!
键入“%destdir%\*.html”
后藤: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关闭
SETLOCAL ENABLEDELAYEDEXPANSION
rem源目录、目标目录、目标目录的以下设置,
rem批处理目录、文件名、输出文件名和临时文件名[如果显示]是名称
我用于测试的rem,故意包含包含空格的名称,以确保
rem确认流程使用这些名称工作。这些将需要改变,以适应您的情况。
设置“sourcedir=u:\your files”
设置“destdir=u:\your results”
设置“filename1=%sourcedir%\q65556186.txt”
设置“filename2=%sourcedir%\q65556186_2.txt”
集合i=0
对于(“%filename1%”中的/F“usebackq”%%a,请执行以下操作(
设置/A i+=1
设置名称u!i!=%%a
)
设置n=%i%
设置s=0
对于(“%filename2%”中的/F“usebackq”%%a,请执行以下操作(
设置/A s+=1
设置文本_u!s!=%%a
)
对于(1,1,%s%)中的/L%%v,请执行>%destdir%\!names\%v!.html“echo!”!文本%%uv!
键入“%destdir%\*.html”
后藤:EOF

[相同的结果]

您可能可以这样做:

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

mh我不确定我是否正确地实现了它。如果我运行该文件,它只会创建一个名为空的.html文件。而其中只有第二个文件的最后一个字符串工作正常!mh,当变量%%v中有空格字符时,某些文本会被截断:我尝试在(“%filename2%”中使用/F“usebackq tokens=*”%%a是的。但是它不起作用,你知道有什么快速的解决方法吗?哦,解决了:我只需要在它起作用之后留出一点空间,但我更喜欢另一个答案,因为它与我的代码有关。
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