简体   繁体   English

Powershell 访问被拒绝

[英]Powershell access Denied

I've got this function, that move a directory to inside another directory, but always gives me the access denied error.我有这个功能,将目录移动到另一个目录中,但总是给我拒绝访问错误。

Any idea what am i doing wrong?知道我做错了什么吗?

Get-ChildItem C:\Scripts\MetadataExport -Directory | ForEach-Object {
    # this will look for a 4-12 digit number in the directory name
    If ($_.Name -match '(?:\b|\D)(\d{4,12})(?:\b|\D)') {
        $destPath = Join-Path $_.Parent.Fullname $Matches[1]

        If (-not (Test-Path -LiteralPath $destPath)) {
            New-Item $destPath -ItemType Directory
        }

        Move-Item -LiteralPath $_.Fullname -Destination $destPath -Force
    }
} 

the error:错误:

   Move-Item : Access to the path 'C:\Scripts\MetadataExport\kwakwala-rosenblum-0172' is denied.
   At C:\Users\User\Documents\ScripsPS1\MetadataExport.ps1:170 char:9
     Move-Item -LiteralPath $_.Fullname -Destination $destPath -Fo ...
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  CategoryInfo          : WriteError: (C:\Scripts\Meta...-rosenblum-0172:DirectoryInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

在此处输入图片说明

So, the goal is to move every folder that part of the name matches with another folder with numeric name.因此,目标是移动名称的一部分与另一个具有数字名称的文件夹匹配的每个文件夹。

Ex:前任:

在此处输入图片说明

The 'kwakwala-rosenblum-0172' folder needs to move to inside '0172' folder. 'kwakwala-rosenblum-0172' 文件夹需要移动到 '0172' 文件夹内。 Just move if the Literal-Path is the same as folder's name.如果 Literal-Path 与文件夹的名称相同,只需移动即可。

I am not sure what you are trying to do but you effectively try to move a folder to a folder with the same name:我不确定您要做什么,但您有效地尝试将文件夹移动到具有相同名称的文件夹:

$Null = New-Item -Path .\0172 -ItemType Directory -Force
$Null = New-Item -Path .\kwakwala-rosenblum-0172 -ItemType Directory -Force
Get-ChildItem -Directory | Foreach-Object {
    if ($_.Name -Match '(?:\b|\D)(\d{4,12})(?:\b|\D)') {
        $destPath = Join-Path $_.Parent.Fullname $Matches[1]
        Write-Host $_.Fullname '-->' $destPath
    }
}

C:\..\0172 --> C:\..\0172
C:\..\kwakwala-rosenblum-0172 --> C:\..\0172

Which is the same as doing this:这与执行此操作相同:

Move-Item -LiteralPath .\0172 -Destination .\0172 -force

Move-Item: The process cannot access the file because it is being used by another process. Move-Item:进程无法访问该文件,因为它正被另一个进程使用。

Meaning that this would likely "resolve" the issue:这意味着这可能会“解决”问题:

if ($_.Fullname -ne $destPath) {
    Move-Item -LiteralPath $_.Fullname -Destination $destPath -Force
}

But, I am not sure what your expectation is.但是,我不确定您的期望是什么。
You might want to explain ( in the question ) how you expect the subfolders to be (re)named.您可能想要解释(在问题中)您希望如何(重新)命名子文件夹。

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

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