生成两个文件夹之间复制的文件的 txt 报告

生成两个文件夹之间复制的文件的 txt 报告

我有一个脚本,可以将音频文件从一个文件夹复制到另一个文件夹。

脚本:

$tot = 0
$files = 'C:\Users\CMG\Desktop\List.txt'
$location = 'C:\Users\CMG\Desktop\Select Dance\'
$destination = 'C:\Users\CMG\Desktop\Select Dance Copy\'

Get-Content $files | % {
 
    $result = Get-ChildItem -Recurse "$location*$_*"

    If($result) {
       gci -recurse "$location*$_*" | Out-File -FilePath "C:\Users\CMG\Desktop\Select Dance Copy\List source file locations.txt" -append
       $musicName = $result.Name        
       $tot+=1       
       Write-Host "$musicName copied"
       Copy-Item $result.FullName -Destination $destination\$($_.Name)              
    }
}

Write-Host " $tot Files were copied! " -ForegroundColor Green

choice /c e /n /m 'Process finished.[E]xit'
        switch ($LASTEXITCODE) {
          1 { ' ' }
        }
        
        if ($LASTEXITCODE -eq "1") {
            break
        }

exit

txt文件中的结果是这样的:

    Directory: C:\Users\CMG\Desktop\Select Dance

Mode                 LastWriteTime         Length Name                                                                                                         
----                 -------------         ------ ----                                                                                                         
-a----        03/12/2021     14:45        6948260 Cheryl Lynn - Got To Be Real (Dj 'S' Bootleg Extended Dance Re Mix).mp3                                      

    Directory: C:\Users\CMG\Desktop\Select Dance

Mode                 LastWriteTime         Length Name                                                                                                         
----                 -------------         ------ ----                                                                                                         
-a----        03/12/2021     14:45        7548241 Electric Light Orchestra - Last Train To London (Bootleg Extended Dance Remix).mp3                        
       

但我想要获得分组目录的结果,例如:

Directory: C:\Users\CMG\Desktop\Select Dance

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        03/12/2021     14:45        6948260 Cheryl Lynn - Got To Be Real (Dj 'S' Bootleg Extended Dance Re Mix).mp3
-a----        03/12/2021     14:45        7548241 Electric Light Orchestra - Last Train To London (Bootleg Extended Dance Remix).mp3

如何在txt文件中生成复制文件的信息并按目录分组,以便您知道文件是从哪个文件夹复制的?

答案1

这有效:

Get-Content $files | % {

  $result = gci -recurse "$location*\*$_*" -Include *.mp3

  If($result) { 
    Add-Content $destination"List source file locations.txt" -Value $result.Fullname
    $musicName = $result.Name    
    $tot+=1       
    Write-Host "$musicName copied"
    Copy-Item $result.FullName -Destination $destination\$($_.Name)
  } 
} 

$List= Get-Content $destination"List source file locations.txt"
       Remove-Item -Path $destination"List source file locations.txt"
       gci -recurse $List | Out-File -filepath $destination"List source file locations.txt"

Add-content命令将选定的记录添加到txt文件中。

Get-content 命令生成了 $list 变量,该变量由 gci 命令读取,该命令按目录对 txt 文件进行分组。

如果您的报告中同一个文件夹出现两次例如。

\musics\soft
  a.mp3
  b.mp3

\musics\hard
  c.mp3
  d.mp3

\musics\soft
  e.mp3
  f.mp3

在行中插入:
$List=Get-Content $destination"List source file locations.txt"| sort | get-unique

我们将有:

\musics\soft
  a.mp3
  b.mp3
  e.mp3
  f.mp3

\musics\hard
  c.mp3
  d.mp3

相关内容