2016-12-05 127 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

相關問題