【发布时间】:2017-04-20 06:26:08
【问题描述】:
我目前有一个批处理文件,它正在遍历一个文本文件并将每一行分配到一个数组中。我想遍历循环并从数组中的每个值中删除一定数量的字符。这可以吗?
@ECHO off
findstr /C:"number" /C:"type" testFile.txt > oneresult.txt
set "file=oneresult.txt"
set /A i=0
timeout /t 1
echo ---------------Results--------------- > results.txt
for /f "tokens=*" %%x in (oneresult.txt) do (
call echo %%x >> results.txt
call set array[%i%]=%%x
set /A i+=1
)
call echo %i% files received >> results.txt
del "oneresult.txt"
所以现在它只是打印从 testFile.txt 中检索到的字符串,然后最终将它们放入 result.txt。我希望来自 testFile.txt 的所有字符串都删除前 10 个字符。如果有更简单的方法请告诉我。到目前为止,这就是我所发现的,但我也是一个批次的菜鸟。
只是在没有数组的情况下弄清楚了,并将答案发布给将来可能正在搜索的任何其他人:
@ECHO off
findstr /C:"number" /C:"type" testFile.txt > oneresult.txt
set /A i=0
timeout /t 1
echo ---------------Results--------------- > results.txt
for /f "tokens=*" %%x in (oneresult.txt) do (
setlocal enabledelayedexpansion
call set print=%%x
call set newprint=!print:~32!
call echo !newprint! >>results.txt
endlocal
set /A i+=1
)
call echo %i% files received >> results.txt
del "oneresult.txt"
【问题讨论】:
-
批处理文件中的所有阵列管理详细信息在this answer 进行了解释。例如:
set array[!i!]=%%x和for %%i in (!i!) do echo !array[%%i]:~32!
标签: batch-file window