powershell - Powershell递归文件名读取并重命名

标签 powershell powershell-2.0

我已经从ftp服务器上下载了使用wget并强制使用ascii编码的目录树,因此现在我拥有很多文件夹和文件名,例如“foo%C3%BC”(某些文件/文件夹已经具有正确的名称,因为它们仅有ascii字符)。

我现在正尝试使用powershell将其转换回utf-8,我尝试编写以下行来完成此操作

Get-ChildItem C:\Users\Administrator\Desktop\folder -Recurse | select BaseName | Rename-Item -NewName {[System.Web.HttpUtility]::UrlDecode{BaseName}}

但这不起作用,并给我以下错误
Rename-Item : Cannot rename because item at '@{BaseName=filename}' does not exist.
At line:1 char:88
+ ... ect BaseName | Rename-Item -NewName {[System.Web.HttpUtility]::UrlDecode{BaseNam ...
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

一遍又一遍(我想每找到一个文件一次)。

在“选择BaseName”之后,该命令一直有效地运行到管道,因此问题出在重命名部分。

有谁知道如何使它工作?

最佳答案

  [Reflection.Assembly]::loadWithPartialName("System.Web") | out-null   
   $data =  Get-ChildItem C:\Users\Administrator\Desktop\folder -Recurse 
   ForEach($dat in $data){
     $newName = [System.Web.HttpUtility]::UrlDecode($dat.Name);
     $dat | Rename-Item -NewName $newName -Force
 }

关于powershell - Powershell递归文件名读取并重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40423144/

相关文章:

powershell - 如何确保输入密码?

c#-4.0 - 使用 Microsoft PowerShell 编辑 Web Config 文件

c# - PowerShell 远程处理 Lync、Active Directory 搜索域 Controller 时出现错误 "-2147016672"

git - 相当于 PowerShell 中的 bash 排序

powershell - PowerShell Hashtable无法正确返回

powershell - 找不到与参数名称 'PipelineVariable' 匹配的参数

memory - 如何检索内存类型(rdimm 或 udimm)?

python - Django 管理员 - 登录

powershell - Powershell 5中的命令Get-WindowsFeature?

powershell - 无法将参数 “address”转换为 “System.Object[]”,值 “DownloadString”键入 “System.Uri”: “Cannot convert the ” System.Object []“