【问题标题】:Rename files to match filenames in another folder重命名文件以匹配另一个文件夹中的文件名
【发布时间】:2021-05-12 06:17:19
【问题描述】:

我有 2 个目录。

D:\test\get\f

D:\test\set\f

get 文件夹和set 文件夹内有相同数量的文件。 set 中的所有文件都是相同的(这是一个普通的图像文件,多次复制以匹配set 文件夹中的文件数量)。

我的问题是,如何将set 文件夹中的所有文件重命名为与get 文件夹中的相同?

例如:

之前

获取文件夹

apple.jpg
mango.jpg
lychee.jpg

设置文件夹

123.jpg
12!.jpg
asdasd.jpg

之后

获取文件夹

apple.jpg
mango.jpg
lychee.jpg

设置文件夹

apple.jpg
mango.jpg
lychee.jpg

【问题讨论】:

  • 您是如何创建set 文件夹的?为什么不解决这个问题而不是事后解决?一些想法浮现在脑海中,但会非常具体且容易被打破......所以更多信息会有所帮助。
  • @KoryGill 谢谢。我只是手动复制并粘贴相同的图像文件以匹配set 文件夹中的文件数。

标签: powershell


【解决方案1】:

一个未优化并假设最佳情况的简单示例。它显示了遍历路径中的文件、获取文件名和重命名文件的基本步骤。

$names = @()
$getPath = "C:\Temp\get"
$setPath = "C:\Temp\set"
Get-ChildItem $getPath |
    Foreach-object{
    $names += $_.Name
}
$i = 0
Get-ChildItem $setPath |
Foreach-object{
    Rename-Item -Path $_.FullName -NewName $names[$i]
    $i++
}

【讨论】:

  • 谢谢。我收到一个错误。 ForEach-Object : Cannot bind parameter 'RemainingScripts'. Cannot convert the "=0" value of type "System.String" to type "System.Management.Automation.ScriptBlock"
  • 好吧,可能是你的 powershell 版本。尝试将该行更改为: [int]$i = 0
猜你喜欢
  • 2012-06-06
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多