简体   繁体   中英

How to move files from specific folders only using powershell

My code below moves databases from one location to another location:

$filters = Get-Content "c:\customerName.txt"
$source = "\\Server1\Databases"
$destination = "\\Server2\Databases"

    foreach($filter in $filters)
        {
        Get-Childitem $source -include *.* -Recurse`
        | ? {!$_.PsIsContainer -and $_.fullname -match $filter} `
        | % { 

    Move-Item  $_.FullName -Destination $destination"\$filter" 
        }
    } 

The code works absolutely fine, but I need to change it so that it doesnt move files from a specific folder ie \\\\Server1\\Databases\\AMG

So am trying to edit the above code as following:

foreach($filter in $filters)
    {
    Get-Childitem $source -include *.* -Recurse | where {$_.source -notlike *"\\Server1\Databases\AMG"*} `
    | ? {!$_.PsIsContainer -and $_.fullname -match $filter} `
    | % { 

Move-Item  $_.FullName -Destination $destination"\$filter" 
    }
}

But if I run the code, it moves everything including the stuff from \\\\Server1\\Databases\\AMG

How can I fix this code to work as it is supposed to? Any ideas?

You need to change:

Get-Childitem $source -include *.* -Recurse | where {$_.source -notlike *"\\Server1\Databases\AMG"*}

To:

Get-Childitem $source -include *.* -Recurse | where {$_.fullname -notlike "*\\Server1\Databases\AMG*"}
  • There is no .source property returned by Get-Childitem . Instead you could use .fullname which is the full path for each file or folder.
  • You should put the * wildcard characters inside the quote marks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM