使用CSV \ TXT文件的PowerShell重命名文件
正如标题所暗示的那样,我正在尝试创建一个powershell脚本来重命名多个文件。该脚本是指CSV文件,该文件包含CSV文件“ A” A”中的字符串。如果结果为真,那么我想从“ B列”重命名文件。
我设法根据字符串使我的代码工作,但这是下一个阶段 - 有人可以帮我吗?
当前代码:
Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue |
Where {($_.Name -like "*blah*")} |
Rename-Item -NewName {[System.IO.Path]::GetFileNameWithoutExtension($_.fullname) + ".BackupFile"}
一如既往地感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下假定您的CSV文件包含扩展名的文件名 - 根据需要进行调整。
构建列A 值键键的CSV,每个值包含对应
列B
值。过滤
get-childitem
用输出where-object
仅处理其名称作为键键的处理文件。-File
添加到Get-ChildItem
下面的调用,以便仅处理文件(也不是目录对象)。将每个匹配文件重命名为相关的Hashtable条目的值。
注意: -Whatif common参数在上面的命令中预览操作。删除
-whatif
确定操作将执行您想要的操作。注意:作为过滤
Get-Childitem
输出的替代方案-Object
事实之后,您可以使用前者的include
参数如下:include $ ht.keys
-include
实际上是 比后滤波解决方案 - 参见这个答案。The following assumes that your CSV file contains the file names with extension - adjust as needed.
Build a hashtable from the CSV whose entries are keyed by
Column A
values and each contain the correspondingColumn B
value.Filter the
Get-ChildItem
output withWhere-Object
to only process files whose name is present as a key in the hashtable.-File
is added to theGet-ChildItem
call below, so as to process files only (not also directory objects).Rename each matching file to the value of the relevant hashtable entry.
Note: The
-WhatIf
common parameter in the command above previews the operation. Remove-WhatIf
once you're sure the operation will do what you want.Note: As an alternative to filtering
Get-ChildItem
output withWhere-Object
after the fact, you could use the former's-Include
parameter as follows:-Include $ht.Keys
-Include
is actually slower than a post-filtering solution - see this answer.