QA200326

  • command
.\delete-files.ps1 -save "*.xlsx, *.docx" -delete "*.pdf, *_bak.docx"
  • file : delete-file.ps1
param (
    [Parameter(Mandatory=$true)][string]$save = "*.xlsx, *.docx",
    [Parameter(Mandatory=$true)][string]$delete = "*_bak.docx",
    [string]$target = "target"
)
## Const
$CONS_BACKUP_FOLDER = "backup"

## Variable
$target_path = Join-Path $PSScriptRoot $target
$temp_path = Join-path $target_path $CONS_BACKUP_FOLDER
$save_params = ($save.Split(",")).Trim() 
$delete_params = ($delete.Split(",")).Trim() 

## move to backup folder
foreach ($itm in $save_params){
    move-item -path ($target_path + "\" + $itm) -Destination $temp_path 
}

## remove in backup folder
foreach ($itm in $delete_params){
    dir -Path $temp_path | where {$_.Name -like $itm} | remove-item
}

## remove file
Dir -Path $target_path -File | Remove-Item

## move file
Move-Item -Path ($temp_path + "\" + "*.*") -Destination $target_path