Display a/v time length (hh:mm:ss) windows 7

I found a post that was supposed to show a/v time length, but it does not show the time: hh:mm:ss.

Can powershell display a/v time length (I have never used powershell)?
If so, can someone show me how to modify the code below to show the time length?
I have used simple batch files in dos, but never anything more than that.
Thanks, Tracey
I pasted the 2nd section into a powershell and there were a lot of errors, but the date, size, (plus others) did show, but not the hh:mm:ss.


$Directory = “D:\My Source Folder”
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {
$Folder = $Shell.Namespace($.DirectoryName)
$File = $Folder.ParseName($
.Name)
$Duration = $Folder.GetDetailsOf($File, 27)
[PSCustomObject]@{
Name = $.Name
Size = "$([int]($
.length / 1mb)) MB"
Duration = $Duration
}
} | Export-Csv -Path “./temp.csv” -NoTypeInformation

$Directory = “D:\My Source Folder”
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Force
ForEach {
$Folder = $Shell.Namespace($.DirectoryName)
$File = $Folder.ParseName($
.Name)
$Duration = $Folder.GetDetailsOf($File, 27)
[PSCustomObject]@{
Name = $.Name
Size = "$([int]($
.length / 1mb)) MB"
Duration = $Duration
}
}
Export-Csv -Path “./temp.csv” -NoTypeInformation

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the </> in your toolbar, you will find it under the gear icon.

How to format code on PowerShell.org

The terrible formatting makes it difficult to tell whether the code is actually broken or if some of the expected symbols such as _ and | were just lost when posting.

Broadly speaking, the code looks correct. Depending on whether or not you have other files in the source folder, I would use a list of file extensions to make sure you’re only getting media files and not trying to get the duration of a Word document or Excel file.

I’ve cleaned it a up a bit, and this works for me:

$MediaFiles = @('*.mp3','*.mp4')

$Directory = 'E:\Temp'
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Include $MediaFiles |
ForEach-Object {
    $Folder = $Shell.Namespace($_.DirectoryName)
    $File = $Folder.ParseName($_.Name)
    $Duration = $Folder.GetDetailsOf($File, 27)
    [PSCustomObject] @{
        Name     = $_.Name
        Size     = "$([int]($_.length / 1mb)) MB"
        Duration = $Duration
    }
} | Export-Csv -Path 'E:\Temp\Media.csv' -NoTypeInformation

1 Like

I pasted the text as copied (replacing the vertical bar with key), but the numbers are the byte size of the files (output shown below/last).

Is this evidence/proof that powershell can NOT display the a/v time-length (hh:mm:ss)?

Please advise.
Thanks, Tracey
Windows 7, Powershell v2.0
dir cmd never shows time-length and Windows Explorer only displays the time-length on the screen.
No media.csv appears in the d:\temp file and the d:\temp folder does exist.
The ‘Place Names’ file is 00:01:38 minutes long and the file size is 1,575,319 bytes

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\Acer> $MediaFiles = @('*.mp3','*.mp4')
PS C:\Users\Acer> $Directory = 'D:\Users\Public\Public.Language\Pronounce it Perfectly in Spanish 2e\Pronounce it Perfec
tly in Spanish 2e.Down.Load'
PS C:\Users\Acer> $Shell = New-Object -ComObject Shell.Application
PS C:\Users\Acer> Get-ChildItem -Path $Directory -Recurse -Include $MediaFiles


    Directory: D:\Users\Public\Public.Language\Pronounce it Perfectly in Spanish 2e\Pronounce it Perfectly in Spanish 2
    e.Down.Load

Mode LastWriteTime Length Name


-a— 8/5/2023 3:19 PM 1575319 [01] Spanish Place Names in the USA.mp3
-a— 8/5/2023 3:30 PM 1722937 [02] English Words Borrowed From Spanish.mp3
-a— 8/5/2023 3:30 PM 2092399 [03] English-Spanish Cognates.mp3
-a— 8/5/2023 3:31 PM 3089863 [04] the Spanish Alphabet.mp3
-a— 8/5/2023 3:37 PM 1925182 [05] the Pure Vowel Sounds.mp3
-a— 8/5/2023 3:38 PM 1584910 [06] Syllables.mp3
-a— 8/5/2023 3:39 PM 1362232 [07] Words in Sequence.mp3
-a— 8/5/2023 3:40 PM 1227958 [08] the Consonants P, T, Q, K, C.mp3
-a— 8/5/2023 3:40 PM 1258816 [09] the Consonants C, Z, S.mp3
-a— 8/5/2023 3:42 PM 1584910 [10] the Consonants Ch, H, F, X.mp3
-a— 8/5/2023 6:25 PM 4184071 [11] the Consonants X, J, G.mp3
-a— 8/5/2023 6:25 PM 7289053 [12] the Consonants G, W.mp3
-a— 8/5/2023 6:26 PM 9486643 [13] the Consonants V, B.mp3
-a— 8/5/2023 6:28 PM 6982558 [14] the Consonant D.mp3
-a— 8/5/2023 6:28 PM 1935607 [15] the Consonant L.mp3
-a— 8/5/2023 6:29 PM 2338429 [16] the Consonants Ll, Y.mp3
-a— 8/5/2023 6:30 PM 2799214 [17] the Consonants M, N, Ñ.mp3
-a— 8/5/2023 6:30 PM 4277896 [18] the Consonants R, Rr.mp3
-a— 8/5/2023 6:31 PM 4896563 [19] Stressed Syllables of Individual Words.mp3

PS C:\Users\Acer> ForEach-Object {
>>     $Folder = $Shell.Namespace($_.DirectoryName)
>>     $File = $Folder.ParseName($_.Name)
>>     $Duration = $Folder.GetDetailsOf($File, 27)
>>     [PSCustomObject] @{
>>         Name     = $_.Name
>>         Size     = "$([int]($_.length / 1mb)) MB"
>>         Duration = $Duration
>>     }
>> }
>> Export-Csv -Path 'D:\Temp\Media.csv' -NoTypeInformation
>>

I would say the biggest problem you currently face is you’re running powershell 2. It’s scary to think you’re still running windows 7 but it’s unbelievable that you haven’t at least upgraded powershell to 5.1. You need .Net 4.5 which you can read about and download Windows Management Framework 5.1 (includes powershell 5.1) here

https://www.microsoft.com/en-us/download/details.aspx?id=54616#:~:text=WMF%205.1%20requires%20Microsoft%20.,the%20instructions%20at%20Installing%20the%20.

2 Likes

I forgot :frowning: to mention I had v2).

BTW I am now in the process of downloading your recommendation.

Is there any information/training (pdf/video) on converting this text to a powershell batch file or powershell script rather than pasting the text into a powershell window?

Thanks, Tracey
I can not imagine me using powershell any more than this because I am not an administrator.
BTW Windows 7 does just fine for me so far :slight_smile:

Maybe it does NOT work on Windows 7.
Thanks anyway, Tracey

$MediaFiles = @('*.mp3','*.mp4')
$Directory = 'D:\Users\Public\Public.Language\Pronounce it Perfectly in Spanish 2e\Pronounce it Perfectly in Spanish 2e.Down.Load'
$Shell = New-Object -ComObject Shell.Application
Get-ChildItem -Path $Directory -Recurse -Include $MediaFiles
ForEach-Object {
    $Folder = $Shell.Namespace($_.DirectoryName)
    $File = $Folder.ParseName($_.Name)
    $Duration = $Folder.GetDetailsOf($File, 27)
    [PSCustomObject] @{
        Name     = $_.Name
        Size     = "$([int]($_.length / 1mb)) MB"
        Duration = $Duration
    }
}
Export-Csv -Path 'D:\Temp\Media.csv' -NoTypeInformation
============================================================
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Acer> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\Acer> $MediaFiles = @('*.mp3','*.mp4')
PS C:\Users\Acer> $Directory = 'D:\Users\Public\Public.Language\Pronounce it Perfectly in Spanish 2e\Pronounce it Perfec
tly in Spanish 2e.Down.Load'
PS C:\Users\Acer> $Shell = New-Object -ComObject Shell.Application
PS C:\Users\Acer> Get-ChildItem -Path $Directory -Recurse -Include $MediaFiles


    Directory: D:\Users\Public\Public.Language\Pronounce it Perfectly in Spanish 2e\Pronounce it Perfectly in Spanish
    2e.Down.Load


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         8/5/2023   3:19 PM        1575319 [01] Spanish Place Names in the USA.mp3
-a----         8/5/2023   3:30 PM        1722937 [02] English Words Borrowed From Spanish.mp3
-a----         8/5/2023   3:30 PM        2092399 [03] English-Spanish Cognates.mp3
-a----         8/5/2023   3:31 PM        3089863 [04] the Spanish Alphabet.mp3
-a----         8/5/2023   3:37 PM        1925182 [05] the Pure Vowel Sounds.mp3
-a----         8/5/2023   3:38 PM        1584910 [06] Syllables.mp3
-a----         8/5/2023   3:39 PM        1362232 [07] Words in Sequence.mp3
-a----         8/5/2023   3:40 PM        1227958 [08] the Consonants P, T, Q, K, C.mp3
-a----         8/5/2023   3:40 PM        1258816 [09] the Consonants C, Z, S.mp3
-a----         8/5/2023   3:42 PM        1584910 [10] the Consonants Ch, H, F, X.mp3
-a----         8/5/2023   6:25 PM        4184071 [11] the Consonants X, J, G.mp3
-a----         8/5/2023   6:25 PM        7289053 [12] the Consonants G, W.mp3
-a----         8/5/2023   6:26 PM        9486643 [13] the Consonants V, B.mp3
-a----         8/5/2023   6:28 PM        6982558 [14] the Consonant D.mp3
-a----         8/5/2023   6:28 PM        1935607 [15] the Consonant L.mp3
-a----         8/5/2023   6:29 PM        2338429 [16] the Consonants Ll, Y.mp3
-a----         8/5/2023   6:30 PM        2799214 [17] the Consonants M, N, Ñ.mp3
-a----         8/5/2023   6:30 PM        4277896 [18] the Consonants R, Rr.mp3
-a----         8/5/2023   6:31 PM        4896563 [19] Stressed Syllables of Individual Words.mp3


PS C:\Users\Acer> ForEach-Object {
>>     $Folder = $Shell.Namespace($_.DirectoryName)
>>     $File = $Folder.ParseName($_.Name)
>>     $Duration = $Folder.GetDetailsOf($File, 27)
>>     [PSCustomObject] @{
>>         Name     = $_.Name
>>         Size     = "$([int]($_.length / 1mb)) MB"
>>         Duration = $Duration
>>     }
>> }
>> Export-Csv -Path 'D:\Temp\Media.csv' -NoTypeInformation

You’ve missed out the pipe | symbols when you’ve copied and pasted it.

This section is effectively one line of code and should be pasted as a block. In fact, you can paste the entire thing as a block, you don’t need to do it one line at a time.

Get-ChildItem -Path $Directory -Recurse -Include $MediaFiles |
ForEach-Object {
    $Folder = $Shell.Namespace($_.DirectoryName)
    $File = $Folder.ParseName($_.Name)
    $Duration = $Folder.GetDetailsOf($File, 27)
    [PSCustomObject] @{
        Name     = $_.Name
        Size     = "$([int]($_.length / 1mb)) MB"
        Duration = $Duration
    }
} | Export-Csv -Path 'E:\Temp\Media.csv' -NoTypeInformation

Alternatively, paste the whole thing into a file (using notepad.exe will be fine) and save it as a ‘.ps1’ file e.g. myScript.ps1

In your PowerShell prompt run the script using the full path, e.g. if you saved it to C:\Temp type
C:\Temp\myScript.ps1
at the prompt, then press return.

You should not see any output if it was successful.

1 Like

I feel so much better knowing there is one less powershell 2.0 instance in the world. :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.