使用CSV \ TXT文件的PowerShell重命名文件

发布于 2025-02-13 15:42:56 字数 404 浏览 0 评论 0 原文

正如标题所暗示的那样,我正在尝试创建一个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"}

一如既往地感谢!

As the title suggests I am trying to create a Powershell script to rename multiple files. The script refers to a csv file which contains a string in a csv file "column A". If the result is true then I would like to rename the file from "Column B".

Ive managed to get my code to work based on a string but this is the next stage - can anyone help me please?

Current Code:

Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue |
  Where {($_.Name -like "*blah*")} |
  Rename-Item -NewName {[System.IO.Path]::GetFileNameWithoutExtension($_.fullname) + ".BackupFile"}

Thanks as always!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2025-02-20 15:42:56

以下假定您的CSV文件包含扩展名的文件名 - 根据需要进行调整。

  • 构建列A 值键键的CSV,每个值包含对应列B 值。


  • 过滤 get-childitem 输出where-object 仅处理其名称作为键键的处理文件。

    • 请注意,Switch -File 添加到 Get-ChildItem 下面的调用,以便仅处理文件(也不是目录对象)。
  • 将每个匹配文件重命名为相关的Hashtable条目的值。

# Build a hashtable from the CSV that with old-name-new-name pairs.
$ht = [ordered] @{} # Note: you can omit [ordered] in this case.
Import-Csv file.csv |
  ForEach-Object { $ht[$_.'Column A'] = $_.'Column B' }

Get-ChildItem -Path C:\ -File -Recurse -ErrorAction SilentlyContinue |
  Where-Object { $ht.Contains($_.Name) } |
  Rename-Item -NewName { $ht[$_.Name] } -WhatIf

注意: -Whatif common参数在上面的命令中预览操作。删除 -whatif 确定操作将执行您想要的操作。

注意:作为过滤 Get-Childitem 输出的替代方案-Object 事实之后,您可以使用前者的 include 参数如下:

  • include $ ht.keys
  • 但是,至少至少powershell 7.2.x, -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 corresponding Column B value.

  • Filter the Get-ChildItem output with Where-Object to only process files whose name is present as a key in the hashtable.

    • Note that switch -File is added to the Get-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.

# Build a hashtable from the CSV that with old-name-new-name pairs.
$ht = [ordered] @{} # Note: you can omit [ordered] in this case.
Import-Csv file.csv |
  ForEach-Object { $ht[$_.'Column A'] = $_.'Column B' }

Get-ChildItem -Path C:\ -File -Recurse -ErrorAction SilentlyContinue |
  Where-Object { $ht.Contains($_.Name) } |
  Rename-Item -NewName { $ht[$_.Name] } -WhatIf

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 with Where-Object after the fact, you could use the former's -Include parameter as follows:

  • -Include $ht.Keys
  • However, up to at least PowerShell 7.2.x, -Include is actually slower than a post-filtering solution - see this answer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文