Windows 批量:读取和更新文件
Windows Batch: read and update file
我不确定标题是否合适,所以我很抱歉..:)
我有一个脚本,需要在文件中保存某种统计数据。
文件看起来像:
No of attempts: x
No of failed attempts: y
仅此而已。现在,我需要制作一个批处理脚本,它将读取这两个数字并相应地更新它们。
我有以下代码,但它不起作用(惊喜,惊喜)。
@echo off
set statfile=statfile.txt
set tmpfile=tmpfile.txt
set attempts=0
set fattempts=0
if exist %statfile% (
for /f "tokens=*" %%i in (%statfile%) do (
echo %%i > %tmpfile%
if %attempts% equ 0 (
for /f "tokens=4" %%j in (%tmpfile%) do set /a attempts=%%j+1
) else (
for /f "tokens=5" %%k in (%tmpfile%) do set /a fattempts=%%k+1
)
)
)
echo No of attempts: %attempts% > %statfile%
echo No of failed attempts: %fattempts% >> %statfile%
我真的是批处理的新手,所以我不确定我是否正确理解了标记。而且我知道这个脚本的逻辑是......好吧,不好,我只是不知道 bash 提供了什么,即使我看了,我也找不到真正有用的东西。
提前致谢!
@ECHO OFF
set statfile=statfile.txt
REM I assume these two lines are just for demo?:
set attempts=0
set fattempts=0
if not exist %statfile% goto :error
REM read file (two lines):
<%statfile% (
set /p att=
set /p fatt=
)
REM remove anything before and including the colon and add delta:
set /a attn=%att:*:=% + %attempts%
set /a fattn=%fatt:*:=% + %fattempts%
REM write new file:
echo No of attempts: %attn% > %statfile%
echo No of failed attempts: %fattn% >> %statfile%
goto :eof
:error
echo Statfile not found
set newvar=%var:*:=%
的解释:(第一个)冒号告诉解释器对变量做一些事情,*:=
是必须做的:全部从头开始(*
) 直到冒号 :
应该被 (=
) 替换为 - 在这种情况下什么都没有。
例如,试试这个:
set "a=This is my string"
echo %a:*my=It's your%
@ECHO Off
SETLOCAL
SET "statfile=q29529242.txt"
set "attempts="
set "fattempts="
FOR /f "tokens=4,5" %%a IN (%statfile%) DO (
IF DEFINED attempts (SET /a fattempts=%%b) ELSE (SET /a attempts=%%a)
)
SET /a attempts+=1
REM IF (condition FOR false) SET /a fattempts+=1
echo No of attempts: %attempts% > %statfile%
echo No of failed attempts: %fattempts% >> %statfile%
GOTO :EOF
我使用了一个名为 q29529242.txt
的文件,其中包含您的数据用于我的测试。
备注:
set
语句始终适用于字符串。 /a
选项将分配的值解释为公式并将结果分配给该值。
set "var=value"
语法可确保行中的尾随 space 不 包含在分配的值中。为变量分配一个空值 "deletes"。请注意,spaces 在作业的 两边都很重要。
for /f
标记选项使用 delims
分隔符解释该行,默认为 space、逗号、分号。文本行被视为由定界符分隔的标记序列-sequences.
于是,上线
No of failed attempts: y
- 否是令牌1
- of 是令牌 2
- 失败是令牌 3
- 尝试:是令牌 4
- y 是令牌 5
你设置'delims=:'然后
- No of failed attempts is token 1
- “y”(不带引号)是标记 2
(请注意,分隔符本身 不包括 )
您可以 select 按数字标记,以逗号分隔。特殊标记 *
表示 "everything in the line after the delimiter following the highest nominated token"。标记被分配给元变量(循环控制变量)的最低 selected,然后按数字顺序分配给每个下一个字母元变量。
请注意,在 "block statement"(一系列带括号的语句)中,值 %var%
被变量的 初始值 替换。有多种方法可以改变这种行为。然而,if defined
语句使用 current
值(它已定义或未定义),因为它在循环中变化。
随后,由于 attempts
最初被清除,在读取第一行后,attempts
被设置为第 4
个标记 (x)。在第二行,attempts
现在已经设置,所以 fattempts
被设置为第 5
个标记 (y)。
我使用了 /a
,因为该值将是数字,这使得赋值不受 spaces 的影响。
见
set /?
来自 set
类 C 语法文档的提示
我不确定标题是否合适,所以我很抱歉..:)
我有一个脚本,需要在文件中保存某种统计数据。
文件看起来像:
No of attempts: x
No of failed attempts: y
仅此而已。现在,我需要制作一个批处理脚本,它将读取这两个数字并相应地更新它们。
我有以下代码,但它不起作用(惊喜,惊喜)。
@echo off
set statfile=statfile.txt
set tmpfile=tmpfile.txt
set attempts=0
set fattempts=0
if exist %statfile% (
for /f "tokens=*" %%i in (%statfile%) do (
echo %%i > %tmpfile%
if %attempts% equ 0 (
for /f "tokens=4" %%j in (%tmpfile%) do set /a attempts=%%j+1
) else (
for /f "tokens=5" %%k in (%tmpfile%) do set /a fattempts=%%k+1
)
)
)
echo No of attempts: %attempts% > %statfile%
echo No of failed attempts: %fattempts% >> %statfile%
我真的是批处理的新手,所以我不确定我是否正确理解了标记。而且我知道这个脚本的逻辑是......好吧,不好,我只是不知道 bash 提供了什么,即使我看了,我也找不到真正有用的东西。
提前致谢!
@ECHO OFF
set statfile=statfile.txt
REM I assume these two lines are just for demo?:
set attempts=0
set fattempts=0
if not exist %statfile% goto :error
REM read file (two lines):
<%statfile% (
set /p att=
set /p fatt=
)
REM remove anything before and including the colon and add delta:
set /a attn=%att:*:=% + %attempts%
set /a fattn=%fatt:*:=% + %fattempts%
REM write new file:
echo No of attempts: %attn% > %statfile%
echo No of failed attempts: %fattn% >> %statfile%
goto :eof
:error
echo Statfile not found
set newvar=%var:*:=%
的解释:(第一个)冒号告诉解释器对变量做一些事情,*:=
是必须做的:全部从头开始(*
) 直到冒号 :
应该被 (=
) 替换为 - 在这种情况下什么都没有。
例如,试试这个:
set "a=This is my string"
echo %a:*my=It's your%
@ECHO Off
SETLOCAL
SET "statfile=q29529242.txt"
set "attempts="
set "fattempts="
FOR /f "tokens=4,5" %%a IN (%statfile%) DO (
IF DEFINED attempts (SET /a fattempts=%%b) ELSE (SET /a attempts=%%a)
)
SET /a attempts+=1
REM IF (condition FOR false) SET /a fattempts+=1
echo No of attempts: %attempts% > %statfile%
echo No of failed attempts: %fattempts% >> %statfile%
GOTO :EOF
我使用了一个名为 q29529242.txt
的文件,其中包含您的数据用于我的测试。
备注:
set
语句始终适用于字符串。 /a
选项将分配的值解释为公式并将结果分配给该值。
set "var=value"
语法可确保行中的尾随 space 不 包含在分配的值中。为变量分配一个空值 "deletes"。请注意,spaces 在作业的 两边都很重要。
for /f
标记选项使用 delims
分隔符解释该行,默认为 space、逗号、分号。文本行被视为由定界符分隔的标记序列-sequences.
于是,上线
No of failed attempts: y
- 否是令牌1
- of 是令牌 2
- 失败是令牌 3
- 尝试:是令牌 4
- y 是令牌 5
你设置'delims=:'然后
- No of failed attempts is token 1
- “y”(不带引号)是标记 2
(请注意,分隔符本身 不包括 )
您可以 select 按数字标记,以逗号分隔。特殊标记 *
表示 "everything in the line after the delimiter following the highest nominated token"。标记被分配给元变量(循环控制变量)的最低 selected,然后按数字顺序分配给每个下一个字母元变量。
请注意,在 "block statement"(一系列带括号的语句)中,值 %var%
被变量的 初始值 替换。有多种方法可以改变这种行为。然而,if defined
语句使用 current
值(它已定义或未定义),因为它在循环中变化。
随后,由于 attempts
最初被清除,在读取第一行后,attempts
被设置为第 4
个标记 (x)。在第二行,attempts
现在已经设置,所以 fattempts
被设置为第 5
个标记 (y)。
我使用了 /a
,因为该值将是数字,这使得赋值不受 spaces 的影响。
见
set /?
来自 set