Batch counter

Asked

Viewed 140 times

0

I am trying to get the lines of a tmp.txt file

set n=0
echo %n%
for /F "tokens=*" %%l in (tmp) do (
    set d[%n%]=%%l
    set /A n+=1
)
echo %d[14]%

but when I execute: 'echo %n%' -> '25' and 'echo %d[14]%'

'ECHO is disabled.'

2 answers

2


To get the total number of lines without saving in variable:

  • At the command line or at bat:
%__AppDir__%\find.exe /c /v "" ^<.\tmp.txt
  • To get the total number of lines and save to a variable on the command line:
for /f %i in ('%__AppDir__%\find.exe /c /v "" ^<.\tmp.txt')do set "_linhas=%i"

To get the total number of lines and save to a variable using a bat:

for /f %i in ('%__AppDir__%\find.exe /c /v "" ^<.\tmp.txt')do set "_linhas=%i"

Command would basically be: Find (find) + /count (count) + /void (ignore/avoid) + "" nothingness + "arquivo"


  • To obtain for each row (even empty/blank) and also save them in a corresponding variable, use EnableDelayedExpansion, where at runtime it is possible to obtain in delay, the value of its numeric variable for your index[n] recem incremented, and replacing "%" for "!" when defining/accessing your %var%/ !_var!...
@echo off

setlocal EnableDelayedExpansion

for /f usebacktokens^=1*delims^=^] %%a in (`
type .\File.txt ^| %__AppDir__%find.exe /n /v "" 
   =;`)do set /a "_n+=1" & >con: call set "_d[%%_n%%]=!_bs!%%b"

:: Acessando todas as linhas salvas em _d[x] :: 
for /l %%l in (1 1 !_n!)do echo\!_d[%%l]! && if %%~l equ !_n! (
     echo\ & echo= Sua linha 14: & echo; "!_d[14]!" & endlocal)

  • Layout option "conventional"
@echo off

cd /d "%~dp0"

setlocal enabledelayedexpansion

for /f usebacktokens^=1*delims^=^] %%a in (`%__AppDir__%find.exe /n /v "" ^<.\File.txt`)do (
     set /a "_n+=1" 
     set "_d[!_n!]=%%b"
    ) 

echo\ !_d[33]!
echo\ !_d[14]!
endlocal 

  • As for the message:

'ECHO está desativado'

> echo /?
Exibe mensagens ou ativa ou desativa o recurso de eco de comando.
 
 ECHO [ON | OFF] 
 ECHO [memssage] 
 
Se usado sem parâmetros, Echo exibe a configuração de eco atual.. . 

1. It is not an error message, it is a message that returns the status for the command echo, and since its variable did not receive a runtime value, the command echo %var% is interpreted by cmd.exe (command interpreter), literally as echo:

echo %var_s_valor%
rem :: o mesmo que ::
echo

2. To delete the text that informs the current state of the command echo in test runs, where you can check if the variable received (or displays) any value correctly, try:

echo. %var%
echo; %var%
echo/ %var%
echo\ %var%
echo= %var%
echo+ %var%
echo( %var%
echo[ %var%
echo: %var%

Obs.: When there is no "string" stored in the variable at runtime, any of the commands echo? above you return a blank line and no longer the message from Status [On|Off], but having some value, the character is not displayed with the variable, only the strings...


>set "%d[14]%=" // nada/sem valor/strings
>echo; %d[14]%
[linha em branco]

>set %d[14]%=linha/strings
>echo; %d[14]%
linha/strings

-3

to contribute...

read bat.

@echo off

echo >result.txt
echo >info.txt
echo >naocadastrado.txt

for /f "tokens=*" %%s in (new14.txt) do (
    
    echo USUARIO ====== %%s
    dsquery user -name  " %%s"
    dsquery user -name  " %%s" >> info.txt
    echo USUARIO ====== %%s  >result.txt
    dsquery user -name  " %%s" >> result.txt 

    for /f "tokens=*" %%a in ('find.exe /c /v "" ^<.\result.txt') do (
       
       if %%a EQU 2  echo "USUARIO NORMAL ========"
       if %%a EQU 1  echo "USUARIO NAO CADASTRADO ========"
       if %%a EQU 1  echo %%s>> naocadastrado.txt
       
      )
    
    echo ====================================================================================================== 
  
   )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.