我正在调度将文件夹中的所有文件重命名为随机数。目前,它们在每个文件名中都有日期,但没有帮助。
以下是我的简单脚本:
$path = "C:\temp\photos\"
$files = Get-ChildItem -Path $path
Foreach ($file in $files) {
$random = Get-Random
$file | Rename-Item -NewName {$Random + $_.extension}
}
但是,我得到了以下错误:
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is
no input. A script block cannot be evaluated without input.
At line:7 char:22
+ Rename-Item -NewName {$Random + $_.extension}
如有任何意见,将不胜感激。
发布于 2020-08-23 01:00:02
根据Olaf的评论,并作了小小的调整:
$path = "C:\temp\photos"
$files = Get-ChildItem -Path $path
ForEach ($file in $files) {
$random = Get-Random
Rename-Item -Path $file.FullName -NewName ($random + $file.Extension)
}
但是,您可能会使它更短一些:
$files = Get-Item -Path "C:\temp\photos\*"
ForEach ($file in $files) {
Rename-Item -Path $file.FullName -NewName ([String]$(Get-Random) + $file.Extension)
}
没有包含任何代码来防止产生重复的随机名称,这超出了您问题的范围。
https://stackoverflow.com/questions/63542103
复制相似问题