powershell重命名脚本-主要功能,希望完成并完全自动化任务



这是我在Stack Overflow上的第一篇文章,我避免寻求帮助,并尽可能自己解决这个问题。我几乎没有编写脚本的经验,但我正在努力学习。我选择这个项目作为开始的地方,感觉我对这个剧本的目标很快就失控了。

我所拥有的是一个功能性的,可能非常臃肿的脚本,它只做了我希望它为我做的一小部分

这是我的任务,在我们100%手动逐个文件之前:

我有一些非常重要的MP3文件,需要从SD卡中复制出来,最终移动到定期备份的共享驱动器中

这些文件的文件名格式为MMDDYYYYMSS_RC-700R.mp3

我们将这些文件存储在数据共享的自己的目录中,并按月份进一步组织,这让我尝试将其作为脚本的一部分添加进来,但这是一个不太重要的功能。

我的目标是安全地将这些文件从SD卡中移出,重命名它们-删除文件的TIME,但如果在同一日期有2个(或更多(文件,则添加按字母顺序递增的迭代计数。我试着把每一步都注释出来,不仅让其他人阅读,而且让自己了解我在代码的每一部分中试图实现的目标。

#Start by Clearing Host and Saving Transcript
Clear-Host
$date = Get-Date -Format 'dddd MM-dd-yyyy'
Start-Transcript -Path "F:Script$date.txt" -NoClobber -Append
#Set locations - SD Card:Import
$Import = "C:UsersDeathDesktopMinutes"
$Source = "F:Minutes"
$Destination = "F:Final"
#Test if $Source Exists, if not Create directory for Files to be transferred to
if(!(Test-Path -Path $Source))  {  
New-Item -ItemType directory -Path $Source
Write-Host "Folder path has been created successfully at: " $Source    
}
else { 
Write-Host "The given folder path $Source already exists"; 
}
#Test if $Destination exists and create if false
if(!(Test-Path -Path $Destination))  {  
New-Item -ItemType directory -Path $Destination
Write-Host "Folder path has been created successfully at: $Destination "}
else { 
Write-Host "The given folder path $Destination already exists"; 
}
#Copy Items from $Import location
Get-ChildItem -Path $Import -Filter *.mp3 | Copy-Item -Destination $Source
#Rename Files adding Dashes between MM DD YYYY and HHMMSS
Get-ChildItem -Path $Source -Filter *.mp3 | Rename-Item -NewName {$_.Name -Replace ('^n*(d{2})(d{2})(d{4})(d{6}).(?:...)(d{3})w','TC $1-$2-$3-$4')}

#Move files into $DestinationYearMonth created - Need to add

#Move files into $NewPathYearMonth created
#Running in to problems with moving the files after they have been renamed - Likely due to 
Get-ChildItem -File -Path $Source -Filter '*.mp3' |
ForEach-Object {
$Year = $_.LastWriteTime.Year
$Month = $_.LastWriteTime.Month
$Monthname = (Get-Culture).DateTimeFormat.GetMonthName($Month)
$ArchDir = "$Destination$Year$Monthname"
if (-not (Test-Path -Path $ArchDir)) { New-Item -ItemType "directory" -Path $ArchDir | Out-Null }
Move-Item -Path $Source*.mp3 -Destination $ArchDir -Verbose
}

#Would like to add a list of files renamed and where they moved to instead of just Enter to exit
Read-Host -Prompt "Press Enter to exit" 

我的目标中有一小部分在其他版本的代码中工作,但这是我功能最强大的一个。

目前存在的问题:

  1. 我还没有弄清楚如何在重命名文件并关闭TIME时安全地迭代在同一天创建的文件-因此,由于它目前正在运行,我让它将TIME留在文件上,我正在手动删除它,并在需要时添加任何字母

  2. 它在第一个文件的月份将所有文件放入$Destination,并将所有文件放在那里(这不是一个大问题,因为这是额外的(

我希望它列出每个移动操作以及任何错误。它现在用$Source&目标文件夹&最后一个GCI-Move操作的-Verbose。

在我证明脚本是100%功能性的之前,我在本地机器上拥有脚本中的所有位置,以让我的老板满意。

我为文字墙感到抱歉&提前感谢您的帮助

这看起来太复杂了。

对于文件部分,它可以像这样简单。。。根据您所陈述的用例:

  1. 我的目标是安全地将这些文件从SD卡中移出

  2. 重命名它们-删除文件的时间,

Clear-Host
$SourcePath = 'C:Temp'
$TargetPath = 'C:TempTempChild'
Get-ChildItem -Path $SourcePath -Filter '*.mp3' | 
ForEach {
$FileName = $PSItem
Try
{
If ((Get-Item -Path "$TargetPath$($FileName.Name)" -ErrorAction Stop))
{
Rename-Item -Path "$TargetPath$($PSItem.Name)" -NewName "$($FileName.Name -replace 'd+_')" -WhatIf
Move-Item -Path $FileName.FullName -Destination $TargetPath -WhatIf
}
}
Catch {Move-Item -Path $FileName.FullName -Destination $TargetPath -WhatIf}
}
# Results
<#
# When the file does not exists
What if: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M1234R.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R.mp3".
# When the file exists
What if: Performing the operation "Rename File" on target "Item: C:TempTempChild4122021143000_RC-M1234R.mp3 Destination: C:TempTempChildRC-M1234R.mp3".
What if: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M1234R.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R.mp3".
#>

您可以在不使用"新建项目"的情况下创建新路径。只需在复制/移动操作上使用-Force开关/参数。

Test-Path -Path "$TargetPathtest1" 
# Results
<#
False
#>
Get-ChildItem -Path $SourcePath -Filter '*.mp3' | 
Copy-Item -Destination "$TargetPathtest1" -Force -WhatIf
# Results
<#
What if: Performing the operation "Copy File" on target "Item: C:Temp4122021143000_RC-M1234R.mp3 Destination: C:TempTempChildtest1".
#>
Get-ChildItem -Path $SourcePath -Filter '*.mp3' | 
Copy-Item -Destination "$TargetPathtest1" -Force 
Test-Path -Path "$TargetPathtest1" 
Get-ChildItem -Path $TargetPath -Filter '*.mp3'
# Results
<#
True
Directory: C:TempTempChild

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          4/8/2021   6:04 PM             39 04122021143000_RC-M12345R.mp3
-a----          4/8/2021   6:04 PM             39 04122021143000_RC-M1234R.mp3
#>

对于

  1. ,但如果在同一日期创建了2个(或多个(文件,则添加按字母顺序递增的迭代计数

我不知道你为什么用字母和数字。因为alpha会将你限制在26个字母以内,所以你最终不得不将这个字符串加倍。当然,数字,你可以看看数字,然后递增1。

这就是我的意思,只是使用你的文件名和文件属性。然而,您可以通过查看混合中的文件属性来执行相同的方法。

Clear-Host
$SourcePath = 'C:Temp'
$TargetPath = 'C:TempTempChild'
Get-ChildItem -Path $SourcePath -Filter '*.mp3' | 
ForEach {
$FileName = $PSItem
Try
{
If (
-Not ((Get-ChildItem -Path $TargetPath -Filter $FileName.Name) -match "(?<=RC-Md+Rd)") -and 
($FileName.Name -replace '_.*') -match 
((Get-ChildItem -Path $TargetPath -Filter $FileName.Name)  -replace '_.*')
)
{
Rename-Item -Path "$TargetPath$($FileName.Name)" -NewName "$(
      $FileName.Name -replace 'R.', 'R1.'
   )" -ErrorAction Stop -WhatIf
Move-Item -Path $FileName.FullName -Destination $TargetPath -Verbose -WhatIf
}
}
Catch
{
$FileToIncrement = (
Get-ChildItem -Path $TargetPath | 
Where-Object -Property Name -Match ($FileName.BaseName -replace 'Rd+')
).FullName 
if($FileToIncrement -match "(?<=RC-Md+R)(?<bv>d+)")
{
Rename-Item -Path $FileToIncrement -NewName (
$FileToIncrement -replace "(?<=RC-Md+R)(d+)", 
("{0:0000}" -f (([int]::Parse($matches.bv)+1)))
) -Verbose -WhatIf
Move-Item -Path $FileName.FullName -Destination $TargetPath -Verbose -WhatIf
}
}
}
# Results - when incrementer does not exist for the matched time string
<#
VERBOSE: Performing the operation "Rename File" on target "Item: C:TempTempChild4122021143000_RC-M12345R.mp3 Destination: C:TempTempChild4122021143000_RC-M12345R1.mp3".
VERBOSE: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M12345R.mp3 Destination: C:TempTempChild4122021143000_RC-M12345R.mp3".
VERBOSE: Performing the operation "Rename File" on target "Item: C:TempTempChild4122021143000_RC-M1234R.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R1.mp3".
VERBOSE: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M1234R.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R.mp3".
#>
# Results - when incrementer exists for the matched time string
<#
What if: Performing the operation "Rename File" on target "Item: C:TempTempChild4122021143000_RC-M12345R1.mp3 Destination: C:TempTempChild4122021143000_RC-M12345R0002.mp3".
What if: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M12345R.mp3 Destination: C:TempTempChild4122021143000_RC-M12345R.mp3".
What if: Performing the operation "Rename File" on target "Item: C:TempTempChild4122021143000_RC-M1234R1.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R0002.mp3".
What if: Performing the operation "Move File" on target "Item: C:Temp4122021143000_RC-M1234R.mp3 Destination: C:TempTempChild4122021143000_RC-M1234R.mp3".
#>

最新更新