簡體   English   中英

PowerShell-重命名本地路徑\\腳本目錄中的所有文件以及所有子文件夾

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM