2016-12-05 134 views
1

我目前有一个批处理文件正在经历一个文本文件并将每行分配到一个数组中。我想循环遍历循环,并从数组中的每个值中删除一定数量的字符。这可能吗?在批处理文件中操作数组中的字符串?

@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个字符。如果有更简单的方法,请让我知道。到目前为止,这是我发现的,但我也是一个批次noob。

就想通了,而不阵列和发布其他人可能会在未来寻找答案:

@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" 
+1

所有阵列管理批处理文件的详细信息解释在[这个答案](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)。例如:'set array [!i!] = %% x' and'for %% i in(!i!)do echo!array [%% i]:〜32!' – Aacini

回答

0
  • 您使用多个电话在你的代码,而无需了解,这些pseudo calls通常用于不需要setlocal enabledelayedexpansion的不同类型的延迟扩展,但要使符号百分比加倍。
  • 中间文件oneresult是不必要的,一个用于解析findtr的输出的/ f就足够了。括号包围所有输出线的
  • 一组可以重定向到RESULTS.TXT

@ECHO off 
set /A i=0 
(
    echo ---------------Results--------------- 
    for /f "tokens=*" %%x in (
    'findstr /C:"number" /C:"type" testFile.txt' 
) do (
    set print=%%x 
    call echo:%%print:~32%% 
    set /A i+=1 
) 
    call echo %%i%% files received 
) > results.txt 

setlocal enabledelayedexpansion以下代码是官能相同

@ECHO off&Setlocal EnabledelayedExpansion 
set /A i=0 
(
    echo ---------------Results--------------- 
    for /f "tokens=*" %%x in (
    'findstr /C:"number" /C:"type" testFile.txt' 
) do (
    set print=%%x 
    echo:!print:~32! 
    set /A i+=1 
) 
    echo !i! files received 
) > results.txt 
+0

@N。 Spivs出于兴趣,您首先检查了我的答案,现在未选中:我的批次是否有任何问题,或者您是否还有其他问题? – LotPings

相关问题