【问题标题】:Batch (CMD) Iterate over two arrays (made out of 2 .txt files)批处理 (CMD) 迭代两个数组(由 2 个 .txt 文件组成)
【发布时间】:2021-04-09 20:42:36
【问题描述】:

我想制作一个脚本,给定 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
)

【问题讨论】:

    标签: arrays loops for-loop batch-file cmd


    【解决方案1】:

    ... 设置数组{!s!}=%%a ...

    创建array{*} 而不是array[*]

    然后

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

    当然,您也可以将数组称为 namestexts 之类的奇怪名称,并使用相同类型的括号,但我很想使用 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
    

    [相同的结果]

    【讨论】:

    • mh 我不确定我是否正确地实现了它。如果我运行该文件,它只会创建一个名称为空的 .html 文件。里面只有第二个文件的最后一个字符串
    • mh,当变量 %%v 中有空格字符时,某些文本会被截断:我试过 for /F "usebackq tokens=*" %%a in ("%filename2%") do 。但它不起作用,你知道有什么快速解决方法吗?
    • 哦解决了:我只需要在 * 之后放一点空间
    【解决方案2】:

    你可以这样做:

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

    【讨论】:

    • 它有效,但我更喜欢其他答案,因为它与我的代码有关。
    猜你喜欢
    • 2018-01-23
    • 2013-12-17
    • 2021-04-25
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多