I'm trying to create a Powershell script that looks for just files with the extension .dgn within a specific directory. Then if it has a character string of "_ch_" in the name of the file it will delete it. Else it will move the file to a new directory.
Function Remove-ExtraneousCADFiles {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true)]
$paths
)
Begin {Clear-Host}
Process {
$char = "_ch_"
$fileName = Split-Path $input -leaf
$destDir = "D:\CRASH\ACCID - Lite Test\"
Write-Output ("fileName= " + $fileName)
Write-Output ("char= " + $char)
Write-Output ("_.Name= " + $_.Name)
#if ($_.contains($char)) {
#if (Where $_.Name -Match $char) {
#if ($_.Name -Match $char) {
if ($_.Name -Like $char) {
Remove-Item $_ -Force -Confirm -WhatIf
Write-Output ("Delete= " + $fileName.Name)
}
else {
#Write-Output ("Move _.FullName= " + $_.FullName)
#Write-Output ("destDir= " + $destDir)
#Write-Output ("_.BaseName= " + $_.BaseName)
#Write-Output ("_.Extension= " + $_.Extension)
#Write-Output ""
Move-Item -Path $_.FullName -Destination ($destDir + $_.BaseName + $_.Extension) -WhatIf
Write-Output ""
}
}
End {}
}
$inputPath = "D:\CRASH\ACCID - Lite Test zzz"
(Get-ChildItem -Path $inputPath -Recurse -Include *.dgn) | Remove-ExtraneousCADFiles
But I can't get the "IF" portion of my script to pass anything to the Remove-Item or Write-Output command. I've try other commands but I'm not having any luck, any help would be greatly appreciated.
These are an example of the files I'm trying to weed out to either delete or move:

Thanks, G
Get-ChildItem -LiteralPath 'D:\Temp' -Recurse -Filter '*.dgn' | ForEach-Object {if($_.Name -match $_.Name -match '^.*_ch_.*$') {<# Code to delete file $_ #>} else {<# Code to move file $_ #>}}$paths(better :$Path(singular)) as the pipeline-binding parameter, so it's better to use it - not$input- in yourprocessblock.