首页 > 解决方案 > Powershell重命名文件不起作用 - 没有错误

问题描述

我正在尝试将文件从一个目录复制到另一个目录并重命名它们。目标文件夹的文件被删除并且文件被复制,但不幸的是我的脚本的重命名部分没有做任何事情。没有显示错误。

#Set variables
[string]$source = "C:\temp\Photos\Original\*"
[string]$destination = "C:\temp\Photos\Moved\"
#Delete original files to avoid conflicts
Get-ChildItem -Path $destination -Include *.* -Recurse | foreach { $_.Delete()}
#Copy from source to destination
Copy-item -Force -Recurse -Verbose $source -Destination $destination

Get-ChildItem -Path $destination -Include *.jpg | rename-item -NewName { $_.Name -replace '-', ' ' }

目前我只是想用空格替换连字符W,但当我可以让它工作时,我还需要从文件名的末尾删除 a 。

示例原始文件名:First-Last-W.jpg

示例所需的文件名:First Last.jpg

标签: regexpowershellpowershell-4.0

解决方案


更改-include参数-filter

Get-ChildItem -Path $destination -Include *.jpg

include 是基于 cmdlet

Get-ChildItem -Path $destination -filter *.jpg

过滤器是基于提供者

了解更多信息


推荐阅读