重命名文件夹中的多个文件,添加行数作为前缀(Powershell)
Rename multiple files in a folder, add a line counts as prefix (Powershell)
我想批量重命名文件夹中的文件,在每个文件名行数前加上“_”作为新名称的前缀。
例子
a.txt
b.txt
c.txt
d.txt
至
1000_a.txt
32_b.txt
199_c.txt
8_d.txt
奖励:用前导零填充行数
到
1000_a.txt
0032_b.txt
0199_c.txt
0008_d.txt
我尝试了什么:
来自添加后缀的命令
Dir | Rename-Item -NewName { $_.basename + "_" + (Get-Content $_).length + "_" + $_.extension}
至
Dir | Rename-Item -NewName { (Get-Content $_).length + "_" + $_.basename + $_.extension}
但是报错
Rename-Item : The input to the script block for parameter 'NewName' failed. Cannot convert value "a" to type "System.Int32". Error: "Input string was not in a correct format."
谢谢
你所做的几乎没问题,错误来自尝试将 int 与 string 连接,PowerShell 尝试类型转换所有元素的类型为 leftmost object in the operation:
The operation that PowerShell performs is determined by the Microsoft .NET type of the leftmost object in the operation. PowerShell tries to convert all the objects in the operation to the .NET type of the first object. If it succeeds in converting the objects, it performs the operation appropriate to the .NET type of the first object. If it fails to convert any of the objects, the operation fails.
由于本例中最左边的对象(.Length
属性)的类型为 int PowerShell 尝试将其余对象转换为 int 失败,例如:
PS /> 1 + 'A'
InvalidArgument: Cannot convert value "A" to type "System.Int32". Error: "Input string was not in a correct format."
这可以通过将返回值类型转换为 sring:
轻松解决
-NewName { [string](Get-Content $_).Length + "_" + $_.BaseName + $_.Extension }
或者例如使用 字符串格式 或 -f
format operator:
-NewName { '{0}_{1}{2}' -f (Get-Content $_).Length, $_.BaseName, $_.Extension }
至于“奖金”,字符串格式参见How do I control the number of integral digits?
这种情况下可以使用{0:0000}
,例如:
(0..1000).ForEach({'{0:0000}' -f $_})
另一方面,如果您有很多冗长的文件,[System.IO.File]::ReadAllLines(...)
可能比 Get-Content
更快:
-NewName { '{0:0000}_{1}' -f [System.IO.File]::ReadAllLines($_).Length, $_.Name }
# OR
$io = [System.IO.File]
-NewName { '{0:0000}_{1}' -f $io::ReadAllLines($_).Length, $_.Name }
我想批量重命名文件夹中的文件,在每个文件名行数前加上“_”作为新名称的前缀。 例子
a.txt
b.txt
c.txt
d.txt
至
1000_a.txt
32_b.txt
199_c.txt
8_d.txt
奖励:用前导零填充行数 到
1000_a.txt
0032_b.txt
0199_c.txt
0008_d.txt
我尝试了什么: 来自添加后缀的命令
Dir | Rename-Item -NewName { $_.basename + "_" + (Get-Content $_).length + "_" + $_.extension}
至
Dir | Rename-Item -NewName { (Get-Content $_).length + "_" + $_.basename + $_.extension}
但是报错
Rename-Item : The input to the script block for parameter 'NewName' failed. Cannot convert value "a" to type "System.Int32". Error: "Input string was not in a correct format."
谢谢
你所做的几乎没问题,错误来自尝试将 int 与 string 连接,PowerShell 尝试类型转换所有元素的类型为 leftmost object in the operation:
The operation that PowerShell performs is determined by the Microsoft .NET type of the leftmost object in the operation. PowerShell tries to convert all the objects in the operation to the .NET type of the first object. If it succeeds in converting the objects, it performs the operation appropriate to the .NET type of the first object. If it fails to convert any of the objects, the operation fails.
由于本例中最左边的对象(.Length
属性)的类型为 int PowerShell 尝试将其余对象转换为 int 失败,例如:
PS /> 1 + 'A'
InvalidArgument: Cannot convert value "A" to type "System.Int32". Error: "Input string was not in a correct format."
这可以通过将返回值类型转换为 sring:
轻松解决-NewName { [string](Get-Content $_).Length + "_" + $_.BaseName + $_.Extension }
或者例如使用 字符串格式 或 -f
format operator:
-NewName { '{0}_{1}{2}' -f (Get-Content $_).Length, $_.BaseName, $_.Extension }
至于“奖金”,字符串格式参见How do I control the number of integral digits?
这种情况下可以使用{0:0000}
,例如:
(0..1000).ForEach({'{0:0000}' -f $_})
另一方面,如果您有很多冗长的文件,[System.IO.File]::ReadAllLines(...)
可能比 Get-Content
更快:
-NewName { '{0:0000}_{1}' -f [System.IO.File]::ReadAllLines($_).Length, $_.Name }
# OR
$io = [System.IO.File]
-NewName { '{0:0000}_{1}' -f $io::ReadAllLines($_).Length, $_.Name }