Can I reduce the code for Powershell workflow

This topic contains 5 replies, has 4 voices, and was last updated by  notarat 3 years, 10 months ago.

  • Author
    Posts
  • #11352

    Biswajit
    Participant

    Can I reduce the code for Powershell workflow?

    ———-CODE———–

    workflow media {

    Suspend-Workflow
    Checkpoint-Workflow

    # 1-Instanse
    Get-ChildItem c:\windows -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv
    -recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media1.csv"

    # 2-Instanse
    Get-ChildItem c:\windows\media -include
    *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv -recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media2.csv"

    # 3-Instanse
    Get-ChildItem c:\ -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media3.csv"

    # 4-Instanse
    Get-ChildItem d:\ -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media4.csv"

    # 5-Instanse
    Get-ChildItem e:\ -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "C:\test\media5.csv"

    # 6-Instanse
    Get-ChildItem f:\ -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media6.csv"

    # 7-Instanse
    Get-ChildItem d:\test -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media7.csv"

    # 8-Instanse
    Get-ChildItem H:\test -include *.avi,*.m4a,*.m4p,*.m4v,*.mobi,*.mov,*.doc,*.mp3,*.mp4,*.mpeg,*.mpg,*.VOB,*.wav,*.wma,*.wmv –
    recurse |
    select Directory,FullName,CreationTime,Length |
    Export-Csv -path "c:\test\media8.csv"

    }

  • #11355

    Don Jones
    Keymaster

    Yes, the -Path parameter of Get-ChildItem can accept a comma-delimited list of paths.

    Get-ChildItem -Path c:\one,c:\two,c:\three

  • #11359

    Biswajit
    Participant

    Thanks but I need separate CSV for per path.

  • #11381

    notarat
    Participant

    [quote=11359]Thanks but I need separate CSV for per path.[/quote]

    You "can" iterate through that list of paths and create the CSV in a separate CSV File(in it's own directory) by using Foreach loops.

  • #11382

    Dave Wyatt
    Moderator

    Something like this is a bit easier to read (and quite a bit shorter, due to the use of a loop.)

    $reports = @{
        'c:\test\media1.csv' = 'c:\windows'
        'c:\test\media2.csv' = 'c:\windows\media'
        'c:\test\media3.csv' = 'c:\'
        'c:\test\media4.csv' = 'd:\'
        'c:\test\media5.csv' = 'e:\'
        'c:\test\media6.csv' = 'f:\'
        'c:\test\media7.csv' = 'd:\test'
        'c:\test\media8.csv' = 'h:\test'
    }
    
    $extensions = '*.avi', '*.m4a', '*.m4p', '*.m4v', '*.mobi', '*.mov', '*.doc', '*.mp3', '*.mp4', '*.mpeg', '*.mpg', '*.VOB', '*.wav', '*.wma', '*.wmv' 
    
    foreach ($entry in $reports.GetEnumerator())
    {
        Get-ChildItem -Path $entry.Value -Include $extensions -Recurse |
        Select-Object Directory, FullName, CreationTime, Length |
        Export-Csv -Path $entry.Key
    }
    
  • #11383

    notarat
    Participant

    [quote=11382]Something like this is a bit easier to read (and quite a bit shorter, due to the use of a loop.)

    $reports = @{
        'c:\test\media1.csv' = 'c:\windows'
        'c:\test\media2.csv' = 'c:\windows\media'
        'c:\test\media3.csv' = 'c:\'
        'c:\test\media4.csv' = 'd:\'
        'c:\test\media5.csv' = 'e:\'
        'c:\test\media6.csv' = 'f:\'
        'c:\test\media7.csv' = 'd:\test'
        'c:\test\media8.csv' = 'h:\test'
    }
    
    $extensions = '*.avi', '*.m4a', '*.m4p', '*.m4v', '*.mobi', '*.mov', '*.doc', '*.mp3', '*.mp4', '*.mpeg', '*.mpg', '*.VOB', '*.wav', '*.wma', '*.wmv' 
    
    foreach ($entry in $reports.GetEnumerator())
    {
        Get-ChildItem -Path $entry.Value -Include $extensions -Recurse |
        Select-Object Directory, FullName, CreationTime, Length |
        Export-Csv -Path $entry.Key
    }
    

    [/quote]

    I went in a different direction using an input file containing the folder to be scanned and the destination folder.

    My input file located in c:\inputfiles consisting of:

    c:\windows,d:\test\media1.csv
    c:\windows\media,d:\test2\media2.csv

    $include=@("*.mp3","*.wav")
    foreach ($i in get-content "c:\inputfiles\tstinput.txt") {
    gci($i -split ',')[0] -include $include -rec | select name | export-csv -path ($i -split ',')[1]
    }
    

You must be logged in to reply to this topic.