读取字符串并使用for存储值

时间:2021-03-05 20:52:44

I have a myFile.TXT, let's say like this:

我有一个myFile.TXT,让我们这样说:

PAR2:VAL2
PAR3:VAL3
PAR4:VAL4
PAR5:VAL5

Using a Batch, i want to take the values to write them to another file, example grant:

使用批处理,我想取值将它们写入另一个文件,示例grant:

MYval_2 VAL2
MYval_3 VAL3
MYval_4 VAL4
MYval_5 VAL5

. i tried this:

。我试过这个:

for /f  "tokens=2 delims=: " %%a in (myFile.txt) do (
echo MYval_2 %%a >> otherFile.txt
echo MYval_3 %%a >> otherFile.txt
echo MYval_4 %%a >> otherFile.txt
echo MYval_5 %%a >> otherFile.txt
)

But as expected, in this way i loop 5 time the file getting this:

但正如预期的那样,我以这种方式循环5次文件得到这个:

val_2 VAL2
val_3 VAL2
val_4 VAL2
val_5 VAL2
val_2 VAL3
val_3 VAL3
val_4 VAL3
val_5 VAL3
val_2 VAL4
val_3 VAL4
val_4 VAL4
val_5 VAL4
val_2 VAL5
val_3 VAL5
val_4 VAL5
val_5 VAL5

can anyone help me to find out a solution? ^^

任何人都可以帮我找到解决方案吗? ^^

2 个解决方案

#1


Below you basically set variables based on myFile.txt then you write their value to otherFile.txt

下面基本上根据myFile.txt设置变量,然后将它们的值写入otherFile.txt

for /f "delims=" %%x in (myFile.txt) do (
  set str=%%x
  set str=%str::==%
  set "%str%"
)

echo %PAR2% >> otherFile.txt
echo %PAR3% >> otherFile.txt
echo %PAR4% >> otherFile.txt
echo %PAR5% >> otherFile.txt

#2


This code assumes you always want to start with MYval_2 and count up by one for each line.

此代码假定您始终希望从MYval_2开始,并为每行计数一次。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET COUNT=2
FOR /F  "tokens=2 delims=: " %%a IN (myFile.txt) DO (
  ECHO MYval_!COUNT! %%a>>otherFile.txt
  SET /A COUNT+=1
)

#1


Below you basically set variables based on myFile.txt then you write their value to otherFile.txt

下面基本上根据myFile.txt设置变量,然后将它们的值写入otherFile.txt

for /f "delims=" %%x in (myFile.txt) do (
  set str=%%x
  set str=%str::==%
  set "%str%"
)

echo %PAR2% >> otherFile.txt
echo %PAR3% >> otherFile.txt
echo %PAR4% >> otherFile.txt
echo %PAR5% >> otherFile.txt

#2


This code assumes you always want to start with MYval_2 and count up by one for each line.

此代码假定您始终希望从MYval_2开始,并为每行计数一次。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET COUNT=2
FOR /F  "tokens=2 delims=: " %%a IN (myFile.txt) DO (
  ECHO MYval_!COUNT! %%a>>otherFile.txt
  SET /A COUNT+=1
)