[英]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
该代码可以正常工作,但是我需要对其进行更改,以便它不会从特定文件夹(即\\\\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
但是,如果我运行代码,它将移动所有内容,包括\\\\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*"}
.source
property returned by Get-Childitem
. Get-Childitem
没有返回.source
属性。 Instead you could use .fullname
which is the full path for each file or folder. 相反,您可以使用.fullname
,这是每个文件或文件夹的完整路径。 *
wildcard characters inside the quote marks. 您应该将*
通配符放在引号内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.