Solved Count number of times looped (Batch)

Custom built pc / Mobo: asus x99-deluxe lga...
August 2, 2016 at 12:17:55
Specs: Windows 7 Pro x64, I7-5820k Haswell-E 6-core @3.3 ghz | 32 GB Corsair DDR4
I have a program that for simplicity I will say converts txt files to csv. It supports command line arguments so i have a simple little batch to run that will do multiple files at a time. It works fine but I would like to add to it.
Here's the code:
for %%a in (*.txt) do convert.exe %%a
pause

This will loop through the .txt files in the directory and convert them to .csv. I would like to add a counter to this that will echo the number of files converted at the end. Is it possible to add a counter and increment it each time the program converts a file then echo the total at the end?

~winipcfg

ASCII Question, Get an ANSI


See More: Count number of times looped (Batch)

Report •

#1
August 2, 2016 at 21:19:42
Yes, but... Ok you can do that, but what's the point? Mainly, you need to know if the conversion was successful, not the number of files that were submitted for conversion.
DIR *.TXT | FIND " File(s)" tells you that, but not whether they were converted (successfully). If "convert.exe" changes the filename, path, or extension, you will need to take a census after conversion to see how many were done. If convert.exe returns an errorlevel, then that's another option to see the ending status. So far, you have not indicated any problems with convert.exe not converting a file, so that's what you need to zero in on: identity of any files NOT being converted, not just a count. If there's no problem, then your initial count of .TXT files should suffice.

Report •

#2
August 3, 2016 at 05:45:13
nbrane,
Thank you for the input. Now that you mention it its a great idea to make a before and after comparison. The converter program does not change the file name; only the file extension. There haven't been any issues with the converter not functioning as it should.

~winipcfg

ASCII Question, Get an ANSI


Report •

#3
August 3, 2016 at 12:35:43
✔ Best Answer
Well, the easiest thing to do would be something like this:
SET cnt=0
FOR %%a IN (*.txt) DO convert %%a &SET /A cnt+=1
@ECHO Convert called %cnt% times

But that just tells you the number of times the loop looped. If you wanted to know the completed conversions, you'd need to compare the number of .txt files before and after.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 3, 2016 at 13:53:41
Thanks Razor. Using a combination of your code and the advice that nbrane gave me I was able to improve the batch. It will count the initial number of txt files then store that number in a variable. Once the conversion process is complete it will compare that number to the number of times the converter looped.
nbrane -

~winipcfg

ASCII Question, Get an ANSI


Report •

#5
August 3, 2016 at 14:53:40
I have no idea about how this converter of yours behaves, but does it set an ErrorLevel if it fails? If so, it'd be easier to just check that.

How To Ask Questions The Smart Way


Report •

#6
August 4, 2016 at 06:36:58
Razor,
I do not believe the converter sets any sort of error level. I put a bit of logic in to test but it's not working as intended. Here it is:

@echo off
::Count number of txt files to be converted
FOR /f %%A in ('dir *.txt ^| find "File(s)"') do set icnt=%%A
::List number of txt files to be converted
echo Number of txt files to be converted = %icnt%
pause
::Loop through txt files in the directory and convert to csv
SET cnt=0
FOR %%B IN (*.txt) DO converter %%B &SET /A cnt+=1
::Display initial number of txt files vs the number converted
IF NOT ERRORLEVEL 1 (ECHO %cnt% of %icnt% files converted) ELSE ECHO Unable to convert 1 or more files
@ECHO %cnt% of %icnt% files converted
pause

The if statement should check the error level but it just displays (number) of (number) of files converted twice. Have I made a coding mistake or does this mean the converter does not set an error level?

~winipcfg

ASCII Question, Get an ANSI

message edited by winipcfg


Report •

#7
August 4, 2016 at 07:15:06
Well, the test is outside of the FOR loop, so it's only going to check the last run of the program. Might be easier to go with a FOR loop like this:
FOR %%B IN (*.txt) DO SET /A cnt+=1 &converter %%B || @ECHO Failed to convert %%B

How To Ask Questions The Smart Way


Report •

Ask Question