首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell获取-随机和重命名项

Powershell获取-随机和重命名项
EN

Stack Overflow用户
提问于 2020-08-22 23:41:22
回答 2查看 2.1K关注 0票数 0

我正在调度将文件夹中的所有文件重命名为随机数。目前,它们在每个文件名中都有日期,但没有帮助。

以下是我的简单脚本:

代码语言:javascript
运行
复制
$path = "C:\temp\photos\"

$files = Get-ChildItem -Path $path

Foreach ($file in $files) {
$random = Get-Random
$file | Rename-Item -NewName {$Random + $_.extension}
}

但是,我得到了以下错误:

代码语言:javascript
运行
复制
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}

如有任何意见,将不胜感激。

EN

Stack Overflow用户

回答已采纳

发布于 2020-08-23 01:00:02

根据Olaf的评论,并作了小小的调整:

代码语言:javascript
运行
复制
$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)
}

但是,您可能会使它更短一些:

代码语言:javascript
运行
复制
$files = Get-Item -Path "C:\temp\photos\*"
ForEach ($file in $files) {
  Rename-Item -Path $file.FullName -NewName ([String]$(Get-Random) + $file.Extension)
}

没有包含任何代码来防止产生重复的随机名称,这超出了您问题的范围。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63542103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档