简体   繁体   English

使用 Import-Csv 重命名各种文件夹中的特定文件

[英]Renaming Specific Files in various folders with Import-Csv

I'm attempting to rename a bunch of files that are located in multiple folders.我正在尝试重命名位于多个文件夹中的一堆文件。 The filenames are not unique because they live in multiple directories.文件名不是唯一的,因为它们存在于多个目录中。 Hence, I'd like to fetch all specific files based on their path and then use the Rename-Item cmdlet to make them unique.因此,我想根据路径获取所有特定文件,然后使用 Rename-Item cmdlet 使它们唯一。

I'm using the Import-Csv cmdlet because I have the Paths and New File Names in a text file (headers are oldPath and NewFileName respectively).我正在使用 Import-Csv cmdlet,因为我在文本文件中有路径和新文件名(标题分别是 oldPath 和 NewFileName)。

I've got this so far:到目前为止我有这个:

    $documentList = @(Import-Csv -Path 'C:\Users\Desktop\testFolder\fileWithPathAndNewFileName.txt') 

$Paths = (ForEach-Object {

    (Gci -Path $documentList.oldPath)  

})  

$Paths | ForEach-Object {Rename-Item -Path $_.FullName -NewName "$($documentlist.newFileName)"}

Unfortunately, this isn't really working, but feels like it's almost there.不幸的是,这并没有真正起作用,但感觉它几乎就在那里。 I think where I'm screwing up is the -NewName parameter.我认为我搞砸的地方是 -NewName 参数。 I'm just not sure how to populate the NewFileName object from my text file correctly.我只是不确定如何从我的文本文件中正确填充 NewFileName 对象。 Any help would be much appreciated.任何帮助将非常感激。 I apologize in advance if this seems somewhat trivial, I unfortunately haven't been consistent with my Powershell.如果这看起来有些微不足道,我提前道歉,不幸的是我没有与我的 Powershell 保持一致。

Your code is a little confused.你的代码有点混乱。 ;-) ... if you have CSV file you should name it *.csv. ;-) ... 如果您有 CSV 文件,您应该将其命名为 *.csv。 If I got you right this should be enough actually:如果我猜对了,这实际上应该足够了:

$documentList = Import-Csv -Path 'C:\Users\Desktop\testFolder\fileWithPathAndNewFileName.txt'

foreach ($document in $documentList) {
    Rename-Item -Path $document.oldPath -NewName $document.NewFileName
}

You iterate over each row in your CSV file and use the value in the cells as input for the Rename-Item cmdlet.您遍历 CSV 文件中的每一行,并将单元格中的值用作Rename-Item cmdlet 的输入。 You need to have the complete path including filename to the file in the column "oldPath" and the NewName in the column "NewName"您需要拥有完整路径,包括“oldPath”列中文件的文件名和“NewName”列中的 NewName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM