查找具有特定文本并重命名的子目录中的文件

huangapple go评论53阅读模式
英文:

Find Files in Subdirectories with Specific Text and Rename

问题

以下是您要翻译的代码部分:

Get-ChildItem -filter *-poster* -recurse | ForEach {Rename-Item $_ -NewName "poster.jpg";}

请注意,这是一段用于在根文件夹中搜索所有子目录中的文件并重命名这些文件的命令,将其重命名为"poster.jpg",但不更改文件扩展名。这个命令将查找文件名中包含"-poster"的文件,并将它们重命名为"poster.jpg"。

英文:

From root folder, I am trying to search all files in subdirectories with "-poster" and rename those files to only poster without changing the file extension. All these files have other text in front of the "-poster" that I want to remove, and all the files are .jpgs.

Would a command such as this work:

Get-ChildItem -filter *-poster* -recurse | ForEach {Rename-Item $_ -NewName "poster.jpg"}

I haven't tried it, as don't want to risk changing other files.

答案1

得分: 1

以下是代码部分的翻译:

Use the -WhatIf common parameter to preview an operation without actually performing it:

使用 -WhatIf 通用参数 来在实际执行之前 预览 操作:

Get-ChildItem -Filter *-poster* -Recurse |
  Rename-Item -NewName poster.jpg -WhatIf

Note:

  • -WhatIf does not test actual runtime conditions.

  • -WhatIf 不会测试实际的运行时条件。

  • That is, if a poster.jpg file already exists in the target directory, the Rename-Item call will fail once -WhatIf is removed.

  • 换句话说,如果目标目录中已经存在一个名为 poster.jpg 的文件,一旦去掉 -WhatIfRename-Item 调用将会 失败

  • To handle this case, you have the following options, depending on your use case:

    • Allow the failure, if you don't expect a preexisting file.

    • 允许失败,如果你不希望有已存在的文件。

    • Ignore the failure, if a preexisting file implies that no action is needed: add -ErrorAction Ignore to the Rename-Item call.

    • 忽略失败,如果已存在的文件意味着不需要采取任何操作:在 Rename-Item 调用中添加 -ErrorAction Ignore

    • Blindly overwrite the existing file, by adding -Force to the Rename-Item call.

    • 通过在 Rename-Item 调用中添加 -Force盲目覆盖 已存在的文件。

    • Create a file with a different name, such as by appending a sequence number, using a delay-bind script block - see below.

    • 创建一个具有不同名称的文件,例如通过添加一个 序列号,使用 延迟绑定脚本块 - 请参见下文。

Get-ChildItem -Filter *-poster* -Recurse |
  Rename-Item -NewName {
    $newBaseName = 'poster.jpg'
    $newBaseName = [IO.Path]::GetFileNameWithoutExtension($newName)
    $newExtension = [IO.Path]::GetExtension($newName)
    $suffix = '' 
    if ([array] $preexisting = Get-ChildItem -File -LiteralPath $_.DirectoryName -Filter "$newBaseName*$newExtension")) {
      $highestNumSoFar = [int[]] ($preexisting.BaseName -replace '^.*\D') | Sort-Object -Descending | Select-Object -First 1
      $suffix = $highestNumSoFar + 1
    }
    $newBaseName + $suffix + $newExtension
  } -WhatIf

希望这些翻译对您有帮助。如果有任何其他问题,请随时提问。

英文:

<!-- language-all: sh -->

Use the -WhatIf common parameter to preview an operation without actually performing it:

Get-ChildItem -Filter *-poster* -Recurse |
  Rename-Item -NewName poster.jpg -WhatIf

Note:

  • -WhatIf does not test actual runtime conditions.

  • That is, if a poster.jpg file already exists in the target directory, the Rename-Item call will fail once -WhatIf is removed.

  • To handle this case, you have the following options, depending on your use case:

    • Allow the failure, if you don't expect a preexisting file.

    • Ignore the failure, if a preexisting file implies that no action is needed: add -ErrorAction Ignore to the Rename-Item call.

    • Blindly overwrite the existing file, by adding -Force to the Rename-Item call.

    • Create a file with a different name, such as by appending a sequence number, using a delay-bind script block - see below.

Get-ChildItem -Filter *-poster* -Recurse |
  Rename-Item -NewName {
    $newBaseName = &#39;poster.jpg&#39;
    $newBaseName = [IO.Path]::GetFileNameWithoutExtension($newName)
    $newExtension = [IO.Path]::GetExtension($newName)
    $suffix = &#39;&#39; 
    if ([array] $preexisting = Get-ChildItem -File -LiteralPath $_.DirectoryName -Filter &quot;$newBaseName*$newExtension&quot;)) {
      $highestNumSoFar = [int[]] ($preexisting.BaseName -replace &#39;^.*\D&#39;) | Sort-Object -Descending | Select-Object -First 1
      $suffix = $highestNumSoFar + 1
    }
    $newBaseName + $suffix + $newExtension
  } -WhatIf

huangapple
  • 本文由 发表于 2023年2月10日 05:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定