简体   繁体   English

如何仅使用Powershell从特定文件夹移动文件

[英]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*"}
  • There is no .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 ,这是每个文件或文件夹的完整路径。
  • You should put the * wildcard characters inside the quote marks. 您应该将*通配符放在引号内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Powershell将这些文件备份到特定文件夹中 - How to backup these files into specific folders using powershell PowerShell - 从特定文件夹复制特定文件 - PowerShell - Copy specific files from specific folders 如何使用PowerShell将文件夹从一个位置移动到另一位置 - How to move folders from one location to another using PowerShell 如何使用 powershell zip 子目录中的特定文件和文件夹 - How to zip specific files and folders in subdirectories using powershell 将匹配特定日期的文件从服务器/文件夹数组移动到本地文件夹(Powershell) - Move files matching specific date from array of servers/folders to a local folder (Powershell) PowerShell - 将特定文件从源子文件夹移动到具有相同子文件夹结构的目标 - PowerShell - Move specific files from source sub folders to destination with identical sub folder structure 仅返回包含使用 Powershell 的文件的文件夹 - Return only folders containing files using Powershell 如何使用 PowerShell 将某些特定文件从我的 Android 手机移动到我的 PC? - How can I move some specific files from my Android phone to my PC using PowerShell? Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中 - Powershell: Move all files from folders and subfolders into single folder 将文件移动到文件夹的 Powershell 脚本 - 如何从 $newFolder 中删除连字符? - Powershell script to move files to folders - how do I remove hyphens from $newFolder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM