Boy, this time I’m really stumped. I am building an advanced function that will go through an archive folder tree and extract every file that has the archive bit set, then write them to output for inspection (and table of contents). This is only the first of four functions I need to build: the next three are a conditional copy for only files ready to archive, then one to clear the archive bit in all files, and last a function to report the total file count and size in GB so I know when it’s time to do the copy to a DVD. I am very close to success, and I have made the core code work for three of these functions. However, all attempts to get an advanced function to perform correctly have been in vain. I have tried everything I can think of, but I’m probably missing something very simple.
Here is thee main code for my “Get-FilesReadyToArchive”
Get-childItem -Path C:\users\peter\Documents\TestArchive -Recurse | where-Object -Property Attributes -EQ archive | Select-Object @{name='File Name';expression={$_.name}},`
@{name='File Size (KB)';expression={$_.length/1e3}}, @{name='Parent Directory';expression={$_.directoryname}}
This works:
PS C:\> Get-childItem -Path C:\users\peter\Documents\TestArchive -Recurse | where-Object -Property Attributes -EQ archive | Select-Object @{name='File Name';expression={$_.name}},`
@{name='File Size (KB)';expression={$_.length/1e3}}, @{name='Parent Directory';expression={$_.directoryname}}
File Name File Size (KB) Parent Directory
--------- -------------- ----------------
test_Alyeska pay stubs 2001 (2).TIF 1148.51 C:\users\peter\Documents\TestArchive\Bird\Owl
test_Income Tax Returns -1995; Jason and Jessica.PDF 253.405 C:\users\peter\Documents\TestArchive\Dog\Terrier
test_Akro Mills catalog.pdf 18832.799 C:\users\peter\Documents\TestArchive \Fish\Trout
Now here is the full script, that just runs with no output:
function Get-FilesReadyToArchive
{
[CmdletBinding()]
Param
(
# Determine path to head directory of archive tree
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$Path
)
Begin
{$AFiles=Get-ChildItem -Path $Path -Recurse
}
Process{ foreach($file in $AFiles){ $file |
Where-Object -Property attributes -EQ archive | Select-Object
@{name='File Name';expression={$_.name}},
@{name='File Size (KB)';expression={$_.length/1e3}},
@{name='Parent Directory';expression={$_.directoryname}} | Out-GridView
}
}
End {
}
This iteration shows a “Foreach” cmdlet. I have tried putting the “Child-Item” cmdlet in the process block, using no “foreach” command, adding explicit write-out commands at the end of the pipeline, and just about everything else. I’ve found lots of mistakes I’ve been making, but this one has me stumped… Peter