Author Posts

November 11, 2013 at 6:37 am

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"

}

November 11, 2013 at 6:46 am

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

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

November 11, 2013 at 7:34 am

Thanks but I need separate CSV for per path.

November 12, 2013 at 6:49 am

[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.

November 12, 2013 at 7:20 am

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
}

November 12, 2013 at 8:12 am

[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]
}