0

I'm looking for a batch or any type of script (CMD/POWERSHELL) that can count files from folders and subfolders with directives.

My files structure is:

C:\Users\XXX\Desktop\TASK\YEAR-MONTH\TaskID\MP4

I need to count the files in every MP4 folder. I tried few scripts that I found around but nothing helps since it counts every single .mp4 in every subfolder and that's not the number I'm looking for.

The current script I'm using:

dir /b *.mp4 /s 2> nul | find "" /v /c > tmp && set /p count=<tmp && del tmp && echo Videos Found: %count%

The batch file should be dropped in the YEAR-MONTH folder and count everything from the MP4 subfolder inside the TaskID folder.

Note: There are 100+ TASKID (Real case when task id is actually task id: 3130) structures (3130/mp4/) <- I need to count .mp4 files in this subfolder going inside every task folder.

I know this is overcomplicated and I would be happy if someone can pull this off!

Regards.

1

this will gather all the mp4 files in subdirs of the dir that the script is in ... and then filter out any that are NOT in a subdir named 'mp4'.

the $PSScriptRoot automatic variable is the dir that the script is started from. drop it into the dir you want to work from and it will do as you described ... get the file count for the mp4 files in the mp4 subdir of the dir that the script is in.

if you want to be able to run the script from any location & give it the target dir, then things will need to be set up with a parameter for that. [grin]

$Filter = '*.mp4'
$StartInDir = $PSScriptRoot
$TargetSubDir = 'mp4'

$MP4_FileCount = @(Get-ChildItem -LiteralPath $StartInDir -Filter $Filter -File -Recurse |
    Where-Object {
        $_.Directory.Name -eq $TargetSubDir
        }).Count

$MP4_FileCount

pause
  • You are the MVP! – ZEED Apr 12 '19 at 14:52
  • @ZEED - you are quite welcome! glad to have helped ... [grin] – Lee_Dailey Apr 12 '19 at 16:04
  • call me noob, but how to add another parameter for example .jpg in $Filter = – ZEED Apr 15 '19 at 13:45
  • @ZEED - you can't put multiple filters in the -Filter parameter. instead, use the -Include parameter - but DO NOT use both. [grin] – Lee_Dailey Apr 15 '19 at 13:47
0

A single line PowewrShell script:

"Videos found: {0}" -f (Get-ChildItem -Path $Env:USERPROFILE\Desktop\TASK\YEAR-MONTH\TaskID\MP4 -Filter *.mp4 -File -Recurse).Count

The proper way in a batch file to process the output of other commands is
wrapping in a for /f not using tmp files.

@Echo off
For /f %%C in ('
dir /b /s "%USERPROFILE%\Desktop\TASK\YEAR-MONTH\TaskID\MP4\*.mp4" 2^>nul ^|find "" /v /c
') Do Echo Videos found: %%C
  • can i replace TaskID with * or something since i have 100+ folders every month Also this is right "%USERPROFILE%\Desktop\TASK\YEAR-MONTH\TaskID\MP4*.mp4" But, i need the batch file to be droped in YEAR-MONTH folder and start counting Sorry if i wasnt clear enough in the first post, but seems lie this is the right direction im looking for. Edited the first post, im really sorry! – ZEED Apr 12 '19 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.