2つの.txtファイルを指定して、最初の.txtファイル内に値として名前が付けられたN個のファイルを作成し、そのファイルに2番目の.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
)
... set array {!s!} = %% a..。
array{*}
代わりに作成するにはarray[*]
次に
for /L %%v in (1,1,%s%) do >"!array[%%v]!.html" echo !array{%%v}!
もちろん、配列を奇妙なものnames
と呼んで、texts
たとえば同じタイプの角かっこを使用することもできますが、角かっこを使用 したいと思いますname:%%i
しtext:%%s
て完全に回避:
。(ファイル名内に存在できないため)name_%%i
およびtext_%%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
【同じ結果】
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加