我可以使用Powershell基于.CSV文件重命名子文件夹中的文件吗 [英] Can I rename files in subfolders based on.CSV file using Powershell

查看:116
本文介绍了我可以使用Powershell基于.CSV文件重命名子文件夹中的文件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于.csv文件重命名一些文件,我成功使用以下代码完成了

I want to rename some files based on .csv file, I succeeded to do so using the following code

Import-Csv "C:\Location\rename.csv" | foreach { Rename-Item -Path $_.Oname -NewName $_.NName }

但这仅重命名了父文件夹中的文件
我试图使其包含子文件夹中的文件,但没有成功

but that renames only the files in the parent folder
I tried to make it includes the files in the sub-folders but in vain

我尝试添加

Get-ChildItem

出现错误,可能我不知道如何将其与导入csv结合

It gets error, probably I don't know how to combine it with the import csv

.csv文件格式为

the .csv file format is

Oname,NName
Name1,NewName1
名称2,新名称2

Oname,NName
Name1,NewName1
Name2,Newname2

推荐答案

这是完成这项工作的一种方法.由于文件可能位于目标目录树中的任何位置,因此回旋的时间更短一些.它做什么...

here's one way to do the job. it's a tad more roundabout since the files may be anywhere in the target directory tree. what it does ...

  • 无法读取CSV文件中的旧文件名/新文件名
  • 设置目标顶级目录
  • 设置文件类型
    如果您只有一种文件类型,则可以在此处和Get-ChildItem行中将其删除.
  • 获取目标目录树中与CSV文件中的ONames匹配的所有文件
  • 通过该文件项进行迭代
  • 获取与当前文件项目名称匹配的OName的索引[项目在数组中的位置]
  • 对该文件进行重命名
    Rename-Item行末尾的-WhatIf用于显示将发生的情况. [ grin ]可以在准备好对样本数据进行真实处理(以后再对真实数据进行处理)时将其删除.
  • fakes reading in a CSV of old/new file names
  • sets the target top directory
  • sets the file type
    if you don't have just one file type, this can be removed here and in the Get-ChildItem line.
  • gets all the files in the target dir tree that match the ONames in the CSV file
  • iterates thru that collection of file items
  • gets the index [position of the item in the array] of the OName that matches the current file item name
  • does a rename on that file
    the -WhatIf on the end of the Rename-Item line is there to show what WOULD happen. [grin] remove it when you are ready to do this for real on your sample data [and later on your real data].

这是代码...

# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
OName, NName
"GWords_2019-02-20.log", "MORE_GWords_2019-02-20.log"
"Two_2019-03-06.log", "Yet another _-_ Two_2019-03-06.log"
"One_GWords_2019-02-27.log", "Slightly different - One_GWords_2019-02-27.log"
'@ | ConvertFrom-Csv

$TargetDir = "$env:TEMP\Testing"
$Filter = '*.log'

$FileList = Get-ChildItem -LiteralPath $TargetDir -Filter $Filter -File -Recurse |
    Where-Object {$_.Name -in $InStuff.OName}

foreach ($FL_Item in $FileList)
    {
    $Index = $InStuff.OName.IndexOf($FL_Item.Name)
    Rename-Item -LiteralPath $FL_Item.FullName -NewName $InStuff[$Index].NName -WhatIf
    }

输出[已重新格式化以提高可读性] ...

output [reformatted for readability] ...

What if: Performing the operation "Rename File" on target 
        "Item: C:\Temp\Testing\GWords_2019-02-20.log
    Destination:
        C:\Temp\Testing\MORE_GWords_2019-02-20.log".
What if: Performing the operation "Rename File" on target
        "Item: C:\Temp\Testing\SubOne\One_GWords_2019-02-27.log
    Destination:
        C:\Temp\Testing\SubOne\Slightly different - One_GWords_2019-02-27.log".
What if: Performing the operation "Rename File" on target
        "Item: C:\Temp\Testing\SubTwo\Two_2019-03-06.log
    Destination:
        C:\Temp\Testing\SubTwo\Yet another _-_ Two_2019-03-06.log".

这篇关于我可以使用Powershell基于.CSV文件重命名子文件夹中的文件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆