Solved Need to find the count of images in a parent folder

September 23, 2019 at 04:36:19
Specs: Windows 10
Hi, I have multiple folders in a parent directory. I need a batch file to find the count of images(jpg,png) to the parent directory which has sub folders too.

See More: Need to find the count of images in a parent folder

Reply ↓  Report •

✔ Best Answer
September 24, 2019 at 17:59:43
Why can't you just:
@echo off & setlocal
cd %parent%
for /f "tokens=1" %%a in ('dir /s *.png *.jpg *.jpeg ^| find "File(s)"') do set ttl=%%a
echo total: %ttl%
::===== end script1
Or if you want them broken down:
@echo off & setlocal enabledelayedexpansion
cd %parent%
for %%z in (png jpg jpeg) do (
set ttl=0
for /f "tokens=1" %%a in ('dir /s *.%%z 2^>nul^| find "File(s)"') do set ttl=%%a
echo total of %%z: !ttl!
)
:: --------- end script2



#1
September 23, 2019 at 05:50:56
how many levels of folders do i have to do? more than 2? does pics4 exitst?

main
 +
 +---pics2
 +---pics3
      +
      +---pics4

i5-6600K[delid]@4.828GHz Core/4.627GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1660Mhz core@1.175v/2236MHz

message edited by hidde663


Reply ↓  Report •

#2
September 23, 2019 at 06:04:57
please replace the C:\FULL_PATH\TO\FOLDER folder with the one you want the script to look in.

& please keep in mind that this script only checks 2 levels, so main folder + sub-folders within the main folder, but not subfolders in the sub sub-folders of the main folder.


test.bat:

@echo off
:presets
cd "C:\FULL_PATH\TO\FOLDER"
set cnt1=0
set cnt2=0
set cnt3=0
set cnt4=0
set fldrs=1
::check parent folder
call :mainloop
:folderloop
for /f "tokens=1" %%a in ('dir/b/AD') do (
	if exist "%%a" (
		cd "%%a"
		set/a fldrs+=1
		call :mainloop
		cd..
	)
)
call :display
exit/b
:mainloop
for /f "tokens=1,2 delims=." %%a in ('dir/b/A-D') do (
	if "%%b"=="jpg" set/a cnt1+=1
	if "%%b"=="png" set/a cnt2+=1
	if "%%b"=="jpeg" set/a cnt3+=1
)
exit/b
:display
set/a cnt4=%cnt1%+%cnt2%+%cnt3%
echo Found %cnt1% .jpg's
echo Found %cnt2% .png's
echo Found %cnt3% .jpeg's
echo Total=%cnt4% pictures (.jpg + .png + .jpeg) in %fldrs% folders.
pause >nul
exit/b


output:

Found 607 .jpg's
Found 55 .png's
Found 0 .jpeg's
Total=662 pictures (.jpg + .png + .jpeg) in 3 folders.

i5-6600K[delid]@4.828GHz Core/4.627GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1660Mhz core@1.175v/2236MHz

message edited by hidde663


Reply ↓  Report •

#3
September 23, 2019 at 06:41:53
Whoa. Instant reply. Thanks bro. It really did the trick. But as my folder structure is clumsy, max the folder structure depth will be upto 7 or 8 sub folders. So if you could do some changes in the code and reply me back, it would be helpful.

I have also posted this doubt on stack overflow page just a while ago.
https://stackoverflow.com/questions...

Thanks in advance.


Reply ↓  Report •

Related Solutions

#4
September 23, 2019 at 07:02:27
RE #3 yes can do endless loops, will just take me a little longer, especially with testing.

i5-6600K[delid]@4.828GHz Core/4.627GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1660Mhz core@1.175v/2236MHz


Reply ↓  Report •

#5
September 23, 2019 at 07:08:36
Am new to batch scripting. If you could do a three or four loops of folder, that would be great. i will replicate it and make some tweaks.

Reply ↓  Report •

#6
September 23, 2019 at 08:31:39
yeah... cant figure it out, will do a re-write tomorrow.

it's something about loops within loops i can't wrap my head around ;)

note to self: try storing all dir's in variables & for /l %%a in (1,1,%dir#%) call :mainloop "%dirname%"

:mainloop
cd "%~1"
""

i5-6600K[delid]@4.828GHz Core/4.627GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1660Mhz core@1.175v/2236MHz


Reply ↓  Report •

#7
September 24, 2019 at 17:59:43
✔ Best Answer
Why can't you just:
@echo off & setlocal
cd %parent%
for /f "tokens=1" %%a in ('dir /s *.png *.jpg *.jpeg ^| find "File(s)"') do set ttl=%%a
echo total: %ttl%
::===== end script1
Or if you want them broken down:
@echo off & setlocal enabledelayedexpansion
cd %parent%
for %%z in (png jpg jpeg) do (
set ttl=0
for /f "tokens=1" %%a in ('dir /s *.%%z 2^>nul^| find "File(s)"') do set ttl=%%a
echo total of %%z: !ttl!
)
:: --------- end script2


Reply ↓  Report •

#8
September 25, 2019 at 01:27:10
Nice, waaay more efficiƫnt.
I was thinking what if his language is not english but hey i think his name gives it away :)

also didnt know about this:
/S Displays files in specified directory and all subdirectories.

calling a loop from within a loop man, my achilles heel. gotta start writing down on paper what im doing at that point.

i5-6600K[delid]@4.828GHz Core/4.627GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1660Mhz core@1.175v/2236MHz


Reply ↓  Report •

#9
September 25, 2019 at 01:38:56
Thanks a lot bro. Helped me a lot.

Reply ↓  Report •

Ask Question