locked
Add 2 different directories, search for files on both then insert into csv RRS feed

  • Question

  • Hello Guys,

    i have 2 folders where i save .wav files and each day we create a directory C:\Dell\YYYYMMDD and D:\Srv1\YYYYMMDD

    the following script is only doing it on one folder so i need help on how i can search on today's folder on both locations (C:\Dell\ , D:\Srv1\) and insert the values into a csv

    $fulldate = Get-Date -format yyyyMMdd
    
    $folder= 'C:\Dell\20200130'
    
    $com = (New-Object -ComObject Shell.Application).NameSpace($folder)
    for($i = 0; $i -lt 64; $i++) {
    $name = $com.GetDetailsOf($com.Items, $i)
    if ($name -eq 'Length') { $lengthattribute = $i}
    }
    $com.Items() |
    ForEach-Object {
    [PSCustomObject]@{
    Name = $_.Name
    Size = $com.GetDetailsOf($_, 1)
    DateCreated = $com.GetDetailsOf($_, 4)
    Length = $com.GetDetailsOf($_, $lengthattribute)
    }
    } |
    Export-Csv -Path c:\Dell\audiolength.csv -Encoding ascii -NoTypeInformation
    Get-Content c:\Dell\Recoridings.csv -Encoding UTF8 | % { $_ -replace """","" } | Out-File "c:\Dell\RecordingsCLEAN.csv" 

    Thanks !!



    • Edited by donweasel Saturday, February 1, 2020 4:56 PM
    Saturday, February 1, 2020 6:00 AM

All replies

  • Please read the following: 

    \_(ツ)_/

    Saturday, February 1, 2020 9:43 AM
  • We do not use the COM object to do this in PowerShell.



    \_(ツ)_/

    Saturday, February 1, 2020 11:09 AM
  • What a strange and unnecessarily complex way of getting a file list. The Powershell way would be something like this:

    $folderList = 'C:\Dell', 'D:\Srv1'
    $fulldate = Get-Date -format yyyyMMdd
    
    $ResultList = foreach ($folder in $folderList) {
        $startPath = Join-Path -Path $folder -ChildPath $fulldate
        Get-ChildItem -Path $startPath -Filter *.wav |
        ForEach-Object {
            [PSCustomObject]@{
                BaseName    = $_.BaseName
                Size        = $_.Length
                DateCreated = $_.CreationTime
            }
        }
    }
    $ResultList |
        Export-Csv -Path c:\Dell\audiolength.csv -Encoding ascii -NoTypeInformation

    Please format your code as code : How to Use the Code Feature in a TechNet Forum Post

    Of course you are allowed to tweak the code to fit your particular needs. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Saturday, February 1, 2020 11:35 AM
    Saturday, February 1, 2020 11:33 AM
  • This is how to do this in PowerShell. It is easier to code and runs faster, is easier to debug and easier to change if changes are necessary

    $folderList = 'C:\Dell', 'D:\Srv1'
    $folderList | 
        ForEach-Object{
            $startPath = '{0}\{1:yyyyMMdd}' -f  $_,[datetime]::Today
            Get-ChildItem -Path $startPath -Filter *.wav
        } |
        Select BaseName, Length, CreationTime |
        Export-Csv c:\Dell\audiolength.csv -NoTypeInformation
    

    PowerShell is puposely designed to support functional programming and this would be the simplest declaritive way to state teh process.


    \_(ツ)_/

    Saturday, February 1, 2020 12:16 PM
  • ok, 
    Saturday, February 1, 2020 4:34 PM
  • What a strange and unnecessarily complex way of getting a file list. The Powershell way would be something like this:

    $folderList = 'C:\Dell', 'D:\Srv1'
    $fulldate = Get-Date -format yyyyMMdd
    
    $ResultList = foreach ($folder in $folderList) {
        $startPath = Join-Path -Path $folder -ChildPath $fulldate
        Get-ChildItem -Path $startPath -Filter *.wav |
        ForEach-Object {
            [PSCustomObject]@{
                BaseName    = $_.BaseName
                Size        = $_.Length
                DateCreated = $_.CreationTime
            }
        }
    }
    $ResultList |
        Export-Csv -Path c:\Dell\audiolength.csv -Encoding ascii -NoTypeInformation

    Please format your code as code : How to Use the Code Feature in a TechNet Forum Post

    Of course you are allowed to tweak the code to fit your particular needs. ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Hello, thanks for the answer, but this one does not work (not exporting to the CSV) and also im looking for the duration length in seconds not the size

    Saturday, February 1, 2020 4:52 PM
  • Hello, thanks for the answer. this one does export just fine (you missed Export-Csv -Path )  from both folders but the info im looking for the length in seconds of the recording not the size.
    Saturday, February 1, 2020 4:55 PM
  • To export from multiple folders just run it in a loop for the folder list.  Why is that difficult?


    \_(ツ)_/

    Saturday, February 1, 2020 6:30 PM
  • Or you can just call the code twice, once for each file.

    function Get-WAVProperties{
        Param(
            $FolderPath
        )
        $fldr = (New-Object -ComObject Shell.Application).NameSpace($folder)
        $fldr.Items() | 
            ForEach-Object{
                $fi = Get-Item $_.Path
                [pscustomobject]@{
                    Name = $fi.Name
                    CreationTime = $fi.CreationTime
                    Size = $fi.Length
                    Length = $fldr.GetDetailsOf($_,27)
                }
            }
    }
    Get-WAVProperties $folder1 | Export-Csv file.csv
    Get-WAVProperties $folder2 | Export-Csv file.csv -Append
    


    \_(ツ)_/

    Saturday, February 1, 2020 6:36 PM
  • Or you can just call the code twice, once for each file.

    function Get-WAVProperties{
        Param(
            $FolderPath
        )
        $fldr = (New-Object -ComObject Shell.Application).NameSpace($folder)
        $fldr.Items() | 
            ForEach-Object{
                $fi = Get-Item $_.Path
                [pscustomobject]@{
                    Name = $fi.Name
                    CreationTime = $fi.CreationTime
                    Size = $fi.Length
                    Length = $fldr.GetDetailsOf($_,27)
                }
            }
    }
    Get-WAVProperties $folder1 | Export-Csv file.csv
    Get-WAVProperties $folder2 | Export-Csv file.csv -Append


    \_(ツ)_/

    Hello @jrv thanks for the answer but how do i declare the folders? will be pass as a parameter?
    Saturday, February 1, 2020 11:45 PM
  • Hello Guys, i think the code is now completed. i took few of each of your suggestions and this is he final

    function Get-WAVProperties{
        Param(
            $FolderPath
        )
        $fulldate = Get-Date -format yyyyMMdd
    $startPath = Join-Path -Path $folder -ChildPath $fulldate
    
    $com = (New-Object -ComObject Shell.Application).NameSpace($startPath)
    for($i = 0; $i -lt 64; $i++) {
    	$name = $com.GetDetailsOf($com.Items, $i)
    	if ($name -eq 'Length') { $lengthattribute = $i}
    }
    $com.Items() |
    	ForEach-Object {
    		[PSCustomObject]@{
    			Name = $_.Name
    			Size = $com.GetDetailsOf($_, 1)
    			DateCreated = $com.GetDetailsOf($_, 4)
    			Length = $com.GetDetailsOf($_, $lengthattribute)
    		}
    	}
    }
    
    Get-WAVProperties 'Y:\'| Export-Csv C:\Dell\file.csv -Encoding ascii -NoTypeInformation 
    Get-Content C:\Dell\file.csv -Encoding UTF8 | % { $_ -replace """","" } | Out-File "c:\Dell\audiolengthCLEAN.csv" 

    if you have any suggestions to improve it please let me know, thanks !


    • Edited by donweasel Sunday, February 2, 2020 1:23 AM
    Sunday, February 2, 2020 1:22 AM