简体   繁体   English

重命名项错误:无法重命名,因为该项不存在

[英]Rename-Item error: Cannot rename because item does not exist

I have the following powershell code, which is attempting to rename all SQL files in a folder, adding a unique prefix to each file eg the first prefix will be '1001 - sp - C - ': 我有以下powershell代码,该代码试图重命名文件夹中的所有SQL文件,并为每个文件添加一个唯一的前缀,例如,第一个前缀将为'1001-sp-C-':

gci -Path 'C:\FolderToLookIn' -Exclude "1*" | ForEach-Object {
    New-Object PSObject -Property @{
        'LineNumber' = $linenumber;
        'Name'       = $_.Name;
        'Extension'  = $_.Extension
    };
    $linenumber++
} | Where-Object {
    $_.Extension -eq ".sql"
} | Rename-Item -NewName {$_.LineNumber.ToString("1000") + ' - sp - C - ' + $_.Name}

However, this produces errors such as the following: 但是,这会产生以下错误:

Rename-Item : Cannot rename because item at '@{Extension=.sql; Rename-Item:无法重命名,因为项目位于'@ {Extension = .sql; LineNumber=22; LineNumber上= 22; Name=sp_Firm_GetAll.sql}' does not exist. 名称= sp_Firm_GetAll.sql}'不存在。

So, how can I add the LineNumber property and use it as a prefix in the Rename-Item command without losing the item itself? 因此,如何在不丢失项目本身的情况下添加LineNumber属性并将其用作Rename-Item命令中的前缀?


Edit 编辑

I have also tried the following, passing through the FullName and using this in the Rename-Item : 我还尝试了以下方法,通过FullName并在Rename-Item使用它:

gci -Path 'C:\FolderToSearch' -Exclude "1*" | ForEach-Object {
    New-Object psObject -Property @{
        'LineNumber' = $linenumber;
        'Name'       = $_.Name;
        'Extension'  = $_.Extension;
        'FullName'   = $_.FullName
    };
    $linenumber++
} | Where-Object {
    $_.Extension -eq ".sql"
} | Rename-Item -Path $_.FullName -NewName {$_.LineNumber.ToString("1000") + ' - sp - C - ' + $_.Name}

However, this errors also: 但是,此错误也会:

Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:3 char:19
+ Rename-Item -Path $_.FullName -NewName {$_.LineNumber.ToString("1000") + ' - sp

As

  • Rename-Item accepts piped input the ForEach isn't necessary and 重命名项接受管道输入,不需要ForEach并且
  • with a [Ref] you can increment a counter inside the -NewName {scriptblock} 使用[Ref]您可以在-NewName {scriptblock}内增加一个计数器

$Folder = 'C:\FolderToLookIn'

$Count = [ref] 0
Get-ChildItem -Path $Folder -Filter *.sql -File |
  Where-Object Name -NotMatch '^\d{4} - sp - C - ' | 
    Rename-Item -Newname {"{0:1000} - sp - C - {1}" -f $Count.Value++,$_.Name}

A sample folder with A.sql, B.sql, C.sql will have this result: 具有A.sql,B.sql,C.sql的示例文件夹将具有以下结果:

> gci *.sql -name
1000 - sp - C - A.sql
1001 - sp - C - B.sql
1002 - sp - C - C.sql

A variant which first obtains the last/highest number from existing files or sets 1000 as the start. 首先从现有文件中获取最后/最高编号的变量,或将1000设置为开始。

$Count = [Ref][math]::Max(1000,
    [int](Get-ChildItem -Path $Folder -Filter *.sql -File|
            Where-Object Name -match '^(\d{4}) - sp - C -' | 
            Select-Object @{n='Count';e={$Matches[1]}} -Last 1).Count)

Get-ChildItem -Path $Folder -Filter *.sql -File |
  Where-Object Name -NotMatch '^\d{4} - sp - C - ' | 
    Rename-item -Newname {"{0:D4} - sp - C - {1}" -f ++$Count.Value,$_.Name}

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

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