0

I have a file with this output:

ANS8000I Server command: 'ru FailedReport'

  Unnamed[1]
------------
       9

 Unnamed[1]
------------
     110

 Unnamed[1]
------------
     101

I need to take those numbers, and assign it to three different variables. I made this script, but I can't make it to work... I always got "0" as result.

@echo off

setlocal enableextensions enabledelayedexpansion

set /A counter=0
SET /A failed=0
SET /A completed=0
SET /A total=0

for /F "tokens=1 skip=4" %%a in (C:\Users\Desktop\Aux.txt) do (

if "%counter%" EQU 5 (set /A failed=%%a)
if "%counter%" EQU 9 (set /A completed=%%a)
if "%counter%" EQU 13 (set /A total=%%a)

set /A counter+=1

)

echo "Failed: " %failed% >> C:\Users\Desktop\Result.txt
echo "Completed: " %completed% >> C:\Users\Desktop\Result.txt
echo "Total: " %total% >> C:\Users\Desktop\Result.txt

Could anyone help me with this? I tried a lot of combinations (variables with !, %, %%), but I still got the same result.

Thanks!!!

2 Answers 2

1
@ECHO OFF
setlocal enableextensions enabledelayedexpansion

set /A counter=0
SET /A failed=0
SET /A completed=0
SET /A total=0

for /F "tokens=1" %%a in (q24152955.txt) do (
 ECHO !counter! %%a
 if !counter! EQU 3 (set /A failed=%%a)
 if !counter! EQU 9 (set /A completed=%%a)
 if !counter! EQU 6 (set /A total=%%a)

 set /A counter+=1

)

echo "Failed: " %failed% 
echo "Completed: " %completed%
echo "Total: " %total%

GOTO :eof

I used a file named q24152955.txt containing your data for my testing.

In delayedexpansion mode, %var% refers to the original value of var - before the loop began execution and !var! is the dynamic value - as it changes.

"something" is never going to be equal to something because the quotes are significant. Quoted values are often used in this context in case the value itself is empty or contains special characters (especially spaces) - "" is a non-empty string.

for /f ignores empty lines. I've included a superfluous echo command to show which lines the for processes. Note that the value of counter is thus the sequential number of non-empty lines in the file, not the actual line number. Note also that a line containing just one (or more) spaces is not an empty line!

Sign up to request clarification or add additional context in comments.

Comments

0
@ECHO off&cls
setlocal enabledelayedexpansion

set "$count=1"
for /f  "delims=" %%a in (C:\Users\Desktop\Aux.txt) do (
 if  !$count!==4 set failed=%%a
 if  !$count!==7 set completed=%%a
 if  !$count!==10 set total=%%a
set /a $count+=1
)

(echo Failed    :  %failed: =%
echo Completed :  %completed: =%
echo Total     :  %total: =%)> C:\Users\Desktop\Result.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.