简体   繁体   中英

Powershell - Copy folders with names matching array

Here's what I'm trying to accomplish

  1. run sql query - result output to txt file
  2. results of txt file to array
  3. get child item- folders and files in the directory if the folder name matches a value from the array
  4. copy items that are returned in step 3 to another directory

The problem I am having is filtering the items in the directory based on the values of the array

gci $sourcepath -recurse | where {$_.name -like $idlist} | Copy-Item -Destination $DestinationHistoryPath

I'm assuming you've already loaded up the array properly. You don't want the -like operator for this particular scenario. You are better off with -in or -Contains . Something like below may work:

Get-ChildItem $sourcepath -Recurse | 
Where-Object {$_.Name -in $idlist} | 
Copy-Item -Destination $DestinationHistoryPath

If you use contains you'll need to switch the operands like:

Get-ChildItem $sourcepath -recurse | 
Where-Object { $idlist -contains $_.Name } | 
Copy-Item -Destination $DestinationHistoryPath

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