[英]PowerShell - rename all files in local path\script directory and all subfolders
因此,嘗試重命名在當前腳本目錄及其所有子文件夾中找到的任何文件。 不想工作。 到目前為止,這就是我要做的...我在做什么錯? 我遇到的問題是它沒有重命名子文件夾中的文件。
<# Renames all files in the working directory to have an extension of .hd #>
$files = Get-ChildItem $PSScriptRoot -Recurse | Where-Object {$_.Extension -match ".jpg|.jpeg|.png|.bmp|.gif|.3gp|.mp4|.webm|.mkv"}
ForEach ($file in $files) {
$filenew = $file.Name + ".hd"
Rename-Item $file $filenew
}
您正在將filesystem
對象傳遞給filesystem
-item的path參數,而不是string path
。
改變這個:
Rename-Item $file $filenew
對此:
Rename-Item -Path $file.fullname -NewName $filenew
Get-ChildItem -Path $Path -Include *.jpg,*.png,*.mp4 -Recurse |
Rename-Item -NewName { $_.Name + '.hd' } -WhatIf
注意:刪除-whatif
以應用重命名操作
無論如何,因此$ file.Name包含擴展名,因此您需要$ file.Basename
$files = Get-ChildItem $PSScriptRoot -Recurse | Where-Object {$_.Extension -match ".jpg|.jpeg|.png|.bmp|.gif|.3gp|.mp4|.webm|.mkv"}
ForEach ($file in $files) {
$filenew = $file.BaseName + ".hd"
Rename-Item $file $filenew
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.