1

I currently have a batch file that is going through a text file and assigning each line into an array. I want to iterate through the loop and remove a certain number of characters from every value in the array. Is this possible to do?

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

So right now it just prints the retrieved strings from testFile.txt and then they are eventually placed into result.txt. I would like all the strings that come from the testFile.txt to have the first 10 characters removed. If there is an easier way please let me know. So far this is what I have found but I am also a bit of a batch noob.

Just figured it out without the array and posting the answer for anyone else that may be searching in the future:

@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"
N. Spivs
  • 11
  • 4
  • 1
    All array management details in Batch files are explained at [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). For example: `set array[!i!]=%%x` and `for %%i in (!i!) do echo !array[%%i]:~32!` – Aacini Dec 05 '16 at 20:54

1 Answers1

0
  • You use several call's in your code without having understood that these pseudo calls are usually used for a different type of delayed expansion not requiring setlocal enabledelayedexpansion, but to double the percent signs.
  • the intermediate file oneresult isn't necessary, one for /f to parse the output of the findstr is sufficient.
  • One set of parentheses enclosing all output lines can redirect to 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 

The following code with setlocal enabledelayedexpansion is functional identical

@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 
  • @N. Spivs Just out of interest you first checked my answer and now unchecked: is there any fault with my batch or do you have further questions? –  Dec 09 '16 at 17:35