MikiBelavista
MikiBelavista

Reputation: 2728

Why is my Move-Item command filtering not detected?

My command to move all rar files

Move-Item -Path * -Filter *.rar -Destination .\Target

I changed a little bit

Move-Item -Path * -Filter .\*.rar -Destination .\NewTarget

Same issue again,rar is not filtered.

Everything is moved to Target(all files). Why?

Upvotes: 1

Views: 401

Answers (1)

Puzo
Puzo

Reputation: 113

I think this should to the work :-)

It is better to filter them before with Get-ChildItem and store them at some variable. And then when you know that you have exact files just doing it with ForEach or ForEach-Object

$Source = "C:\Users\Puzo\Desktop\FolderA"
$Destination = "C:\Users\Puzo\Desktop\FolderB"

$FilesToMove = Get-ChildItem -Path $Source -Filter "*.rar" -Recurse
$i = 1

ForEach ($File in $FilesToMove)
{
    Move-Item -Path $File.FullName -Destination ("$Destination\" + $File.Name)
    Write-Host ('File ' + $File.Name + 'was moved.') -ForegroundColor Yellow
    $i++
}

Write-Host "$i files was moved!" -ForegroundColor Green

Upvotes: 2

Related Questions