Powershell 批量重命名文件-从列表中读取旧名称和新名称

Powershell 批量重命名文件-从列表中读取旧名称和新名称,powershell,Powershell,我有一大堆需要重命名的文件。我需要通过从文件中读取当前名称和新名称(当前在Excel中,但显然可以更改为CSV或其他格式)来实现这一点 为了更好地解释这一点,一些现有姓名有账号,一些有申请号,一些有身份证号,一些有姓名和姓氏 使用Excel中的自定义项和公式,我设法从每个文件名中提取足够的数据,以将其与我们的数据库相匹配,现在我有了每个人的ID号-我需要将文件命名为ID号,以便我们可以将其上载到我们的系统,并且可以正确地对其进行索引 因此,我需要PS读取文件\u list.txt,然后根据列表中

我有一大堆需要重命名的文件。我需要通过从文件中读取
当前名称
新名称
(当前在Excel中,但显然可以更改为CSV或其他格式)来实现这一点

为了更好地解释这一点,一些现有姓名有账号,一些有申请号,一些有身份证号,一些有姓名和姓氏

使用Excel中的自定义项和公式,我设法从每个文件名中提取足够的数据,以将其与我们的数据库相匹配,现在我有了每个人的ID号-我需要将文件命名为ID号,以便我们可以将其上载到我们的系统,并且可以正确地对其进行索引

因此,我需要PS读取
文件\u list.txt
,然后根据列表中的
当前名称找到该文件,并将其重命名为列表中的
新名称


我知道如何通过指定一个标准名称和序列号来批量重命名文件(本网站上有许多这样的帖子),但不知道如何从文件中读取名称。

如果使用
Get ChildItem
cmdlet检索文件,您将在
name
属性中找到文件名:

Get-ChildItem | Rename-Item -NewName { # use $_.Name to get the new name from your list }

您可以使用
Get Content
cmdlet或
Import CSV
读取文件\u list.txt(如果是CSV文件)。

如果使用
Get ChildItem
cmdlet检索文件,您将在
Name
属性中找到文件名:

Get-ChildItem | Rename-Item -NewName { # use $_.Name to get the new name from your list }

您可以使用
Get Content
cmdlet或
Import CSV
读取文件\u list.txt(如果是CSV文件)。

使用带有两列的CSV,分别用于
路径
新名称

Path,NewName "C:\folder\ABC123.txt","ID001.txt" "C:\another_folder\RandomFile001.txt","ID002.txt"
将带有两列的CSV用于
路径
新名称

Path,NewName "C:\folder\ABC123.txt","ID001.txt" "C:\another_folder\RandomFile001.txt","ID002.txt"
我也一直在研究这个问题,但我总是想让事情变得更好。 这个简短的sciprt所做的是获取目录和子目录中的所有文件并重命名它们

创建可用于所有函数的全局变量。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
创建要重命名的文件/目录列表。然后,我们让PowerShell打开Excel并允许您编辑CSV。关闭后,脚本将再次启动。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
现在,通过编辑的列表,我们可以设置要重命名的结构。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
现在完成重命名列表后,我们可以重命名所需的文件。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
调用函数

Function GOGO{
Variables
CreateList
CreateRenameList
Rename
}
GOGO

我也一直在研究这个问题,但我总是想让事情变得更好。 这个简短的sciprt所做的是获取目录和子目录中的所有文件并重命名它们

创建可用于所有函数的全局变量。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
创建要重命名的文件/目录列表。然后,我们让PowerShell打开Excel并允许您编辑CSV。关闭后,脚本将再次启动。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
现在,通过编辑的列表,我们可以设置要重命名的结构。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
现在完成重命名列表后,我们可以重命名所需的文件。

Function Variables{
$Global:CreateListFile="F:\RemoveFiles\"
$Global:ListofFilesCSV="F:\FileList.csv"
$Global:ListofFilesCSV_NewName="F:\FileList2.csv"
$Global:RenameCSV="F:\Rename.csv"
}
Function CreateList{
# Create List File using .Csv
Get-ChildItem $Global:CreateListFile -Recurse | Select FullName | Export-Csv $Global:ListofFilesCSV -NoTypeInformation
#Get-Content $Global:ListofFilesCSV
}
Function CreateRenameList{
$File = Import-CSV $Global:ListofFilesCSV
$File2 = Import-CSV $Global:ListofFilesCSV
$i = 0
$File | ForEach `
{
  $_ | Add-Member -type NoteProperty -name NewName -value $File2[$i].FullName
  $i++
}
$File | Export-CSV $Global:RenameCSV -notype
#Get-Content $Global:RenameCSV
(Start-Process EXCEL "$Global:RenameCSV" -PassThru).WaitForExit()
}
Function Rename{
Import-Csv $Global:RenameCSV | 
ForEach { Rename-Item -Path $_.FullName -NewName $_.NewName }
}
调用函数

Function GOGO{
Variables
CreateList
CreateRenameList
Rename
}
GOGO