简体   繁体   English

使用来自 csv 文件的新名称重命名目录中的文件

[英]Renaming files in directory using new names from csv file

I'm looking for help in creating a script that can rename all of the files contained in a directory to a table referenced in a csv file.我正在寻求创建脚本的帮助,该脚本可以将目录中包含的所有文件重命名为 csv 文件中引用的表。 For example, I have a folder with random file names and I also have a csv file with the current file names in column A, and what the actual file name should be changed to in column B.例如,我有一个随机文件名的文件夹,我还有一个 csv 文件,A 列中有当前文件名,而实际文件名应该在 B 列中更改为什么。

I thought about using something along the lines of:我考虑过使用以下内容:

get-childitem *pdf -force | foreach { rename-item $_ $_.Name.Replace((Import-Csv -path .\\List.csv).filename, (Import-Csv -Path .\\List.csv).newfilename) }

Note that filename & newfilename refer to column A and B respectively.请注意,文件名文件名分别A列和B列。

Nothing really happens when I run this script.当我运行这个脚本时,什么都没有发生。 I'm a novice with powershell so I've come to the limits of my knowledge of how to accomplish this task.我是 powershell 的新手,所以我已经达到了如何完成这项任务的知识极限。 Any and all help will be greatly appreciated!任何和所有的帮助将不胜感激!

Thank you all!谢谢你们!

You can do the following if you are in the directory that contains the target files and the CSV file.如果您在包含目标文件和 CSV 文件的目录中,您可以执行以下操作。 This assumes you have headers in your CSV named filename and newfilename .这假设您的 CSV 中有名为filenamenewfilename

$TargetDir = Resolve-Path -Path .
Import-Csv -Path .\List.csv | Foreach-Object {
    $Src = Join-Path -Path $TargetDir -ChildPath $_.filename
    $Dst = Join-Path -Path $TargetDir -ChildPath $_.newfilename
    Rename-Item -Path $Src -NewName $Dst
}

If the target directory is another location, you can simply modify the Join-Path statements to use -Path .如果目标目录是另一个位置,您可以简单地修改Join-Path语句以使用-Path

$TargetDir = 'c:\folder'
Import-Csv -Path .\List.csv | Foreach-Object {
    $Src = Join-Path -Path $TargetDir -ChildPath $_.filename
    $Dst = Join-Path -Path $TargetDir -ChildPath $_.newfilename
    Rename-Item -Path $Src -NewName $Dst
}

Note: If the CSV file does not contain headers, you will need to either utilize the -Header switch in Import-Csv like in Glenn's helpful answer or add the column headers to the CSV.注意:如果 CSV 文件不包含标题,您将需要像Glenn 的有用答案一样使用Import-Csv-Header开关,或者将列标题添加到 CSV。

Assuming there's no header in the .csv and it only contains the file names, this might work for you:假设 .csv 中没有标题并且它只包含文件名,这可能对您有用:

# Only import the CSV once
$csvData = Import-Csv .\List.csv -Header "Old","New"

# Remove the -WhatIf after testing
Get-ChildItem | ForEach-Object { 
    $file = $_
    $csvData | Where-Object { 
        $_.Old -eq $file.Name 
    } | ForEach-Object { 
        Rename-Item -Path $file -NewName $_.New -WhatIf 
    } 
}

Or a more concise implementation:或者更简洁的实现:

$csvData = Import-Csv .\List.csv -Header "Old","New"
dir | %{ $file = $_; $csvData | ?{ $_.Old -eq $file.Name } | %{ Rename-Item -Path $file -NewName $_.New -WhatIf } }

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

相关问题 使用备用BaseName查找(CSV)名称重命名文件 - Renaming files using alternate BaseName lookup (CSV) names 使用txt文件中的字符串重命名文件 - Renaming files using a strings from a txt file 使用文本文件中的列表重命名目录中的文件 - Renaming files in a directory with a list in Text file 使用一个文件中的 CSV 标头名称创建具有不同标头名称的新文件 - Using CSV header names from one file to create new file with different headers name 遍历CSV,重命名文件并移至PowerShell中的其他目录 - Looping through CSV, renaming files and moving to different directory in PowerShell 将目录中的csv文件合并到单个csv文件PowerShell中 - Merges csv files from directory into a single csv file PowerShell 尝试将具有特定文件名的文件从根目录移动到子文件夹 - Trying to move files with specific file names from root directory to a subfolder 如何从文本文件中的名称列表中批量重命名目录中的文件 - How to batch rename files in a directory from a list of names in a text file 使用 Powershell 将 2 个 CSV 文件中的数据合并为 1 个新的 CSV - Combine data from 2 CSV files into 1 new CSV using Powershell 解析目录中的文件名以使用时间戳输出到csv? - Parse file names in directory to output to csv with timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM