如何批量将每个字符串放入txt文件中的变量

时间:2019-05-26 00:34:53

标签: batch-file text cmd

我想知道是否有一种方法可以将每个字符串从批处理语言的txt文件中放入变量,例如:

Alim.txt文件:

Instructions for updating:
Colocations handled automatically by placer.
2019-05-26 06:00:59.907030: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-05-26 06:00:59.930062: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1992000000 Hz
2019-05-26 06:00:59.931310: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x740fd20 executing computations on platform Host. Devices:
2019-05-26 06:00:59.931349: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
('police', ' : ', '99.86363053321838')
('pilot', ' : ', '0.13637046795338392')
('firefighter', ' : ', '1.232255097960433e-06')
time required for executing this fuction :  6.337834449000184
memory use: 0.4300689697265625
CPU usage :  0.0
         1 function calls in 0.000 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

结果:

381b4222-f694-41f0-9685-ff5bb260df2e   Utilisation_normale

8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c   Performances_élevées

a1841308-3541-4fab-bc81-f71556f20b4a   Économie_d'énergie

,如果可能的话,请加上计数

以一种简单的方式,我在Batchl中不是很好。我将不胜感激

1 个答案:

答案 0 :(得分:0)

您可以使用以下脚本完成此操作:

@echo off
setlocal enabledelayedexpansion
set _count=0
for /f "tokens=1* delims= " %%a in ('type Alim.txt') do (
    set /a _count=!_count!+1
    set var!_count!=%%a
    set /a _count=!_count!+1
    set var!_count!=%%b
)

说明

  • setlocal enabledelayedexpansion启用延迟扩展,以允许在for循环内设置变量。
  • set _count=0创建一个用于设置变量的计数器变量。
  • type Alim.txt打印Alim.txt文件的内容。
  • for /f "tokens=1* delims= " %%a in ('...') do遍历Alim.txt中的每一行,按空格分隔并将这些部分保存到%%a%%b
  • set /a _count=!_count!+1递增_count变量。
  • set var!_count!=%%aset var!_count!=%%b根据当前的_count变量(例如:var1=381b4222-f694-41f0-9685-ff5bb260df2e)设置一个变量。

供参考