从不同的文件中批量读取,并将每个两个字符设置为一个变量



我有一个批处理文件和一个文本文件,我想从该文本文件中获取每两个字符,并将其另存为批处理文件中的变量作为变量

aka

TXT.bat

[][][]

批次.bat

px1=[] px2=[] px3=[]

我想为一定数量的行和 b 数量的双字符执行此操作。

@echo off
setlocal EnableDelayedExpansion
set amount=8
set i=0
set "first="
rem I have a Text file
for /F "delims=" %%a in ('cmd /U /C type textFile.txt^| find /V ""') do (
   rem I want to get every two characters from that file...
   if not defined first (
      set "first=%%a"
   ) else (
      rem and save it as a variable in the batch file...
      set /A i+=1
      set "px!i!=!first!%%a"
      set "first="
      rem and repeat until the file reaches set amount of variables
      if !i! equ %amount% goto continue
   )
)
:continue
set px

文本文件.txt:

[][][]
abcdef
1234567890

输出:

px1=[]
px2=[]
px3=[]
px4=ab
px5=cd
px6=ef
px7=12
px8=34

在你的文件上试试这个,上面写着"file.txt"

它将列出变量,然后暂停。

这使用名为 repl.bat 的帮助程序批处理文件 - 从以下位置下载:https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

repl.bat放在与批处理文件相同的文件夹中或路径上的文件夹中。

@echo off
setlocal enabledelayedexpansion
type "file.txt"|repl "(..)" "$1rn" x >file.tmp
set c=100
for /f "delims=" %%a in (file.tmp) do (
set /a c+=1
set num=!c:~-2!
set "px[!num!]=%%a"
)
set px[
del file.tmp
pause

最新更新