我要求在Windows批处理文件中从文本文件中读取第一个可用行,将其传递给变量并将名称\ line标记为已使用
该文件的示例如下.
苹果
梨
橙
该脚本将以'apple'开头,将'apple'传递给稍后在脚本中使用的变量(我知道该怎么做),然后将该行写回读取&apple,'&'作为标记说它已被使用.
该文件将如下所示:
&苹果
梨
橙
下次运行批处理文件时,它将采用'pear',将其传递给变量并使用&标记它,使其看起来像:
&苹果
和梨
橙
我开始尝试找到'&',然后尝试移动到下一行,但是在经过大约12个小时的尝试后我失败了.这是我到目前为止所做的......并不多:
for/f"tokens = 1"%l in('name.txt')do(Find/v"&"/ v"^ ---- ^ $")(For/F%n in(%l)do (设置NewName =%n))
谢谢
运行此功能the.file将依次修改每一行;
@echo off
setlocal enabledelayedexpansion
type nul > the.file.temp
set last=
for /F "tokens=*" %%A in (the.file) do (
set line=%%A
if "!line:~0,1!" neq "&" if "!last!" equ "" (
set last=!line!
set line=^&!line!
)
echo !line! >> the.file.temp
)
echo last value is !last!
type the.file.temp > the.file
Run Code Online (Sandbox Code Playgroud)
(如果该行不以&并且该变量last为空,则将该行放入last&modify line并使用前导&.始终附加line到临时文件,完成后重命名)