文件夹中的文件编号

Number Files in Folder

我必须重命名 Kodi(家庭影院程序)正确列出的几集。我有一个文件夹,只想逐个文件重命名为 1x011x021x03

这是我到目前为止得到的结果,但它似乎不起作用。

谁能帮我解决这个问题?

$path = Read-Host "please Path!"
$files = gci $path
$count = 0
$files | Rename-Item -NewName {"1x0"+($count+1)+".mkv"}

现在知道了:

$path = Read-Host "please Path!"

$i = 0
Get-ChildItem $path | ForEach-Object {
  $extension = $_.Extension
  $newName = "1x0{0:d1}{1}" -f $i, $extension
  $i++
  Rename-Item -Path $_.FullName -NewName $newName
}

表达式$count+1$count的值,加1,结果returns。变量的值保持不变,因此每次重命名操作都使用相同的名称。

考虑使用 for 循环,因为无论如何您都想对文件进行编号:

$files = @(Get-ChildItem $path)
for ($i = 0; $i -lt $files.Count; $i++) {
  $newname = "1x{0:d2}{1}" -f ($i+1), $files[$i].Extension
  Rename-Item $files[$i].FullName -NewName $newname
}