dropbox1349

Spostare gruppi di files sulla base di un nome comune presente in un file di testo

Feb 4th, 2021 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Video Youtube
  2. https://youtu.be/QjC2wI0EQx8
  3.  
  4. Codice Powershell
  5. ------------------------------------------------------------------------------
  6.  
  7. $sourcePath = 'D:\Test'
  8. $inputFile  = Join-Path -Path $sourcePath -ChildPath 'text.txt'
  9. # because the title may contain invalid characters for a folder name like ':'
  10. # create a regex to remove those if applicable
  11. $invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
  12.  
  13. Import-Csv -Path $inputFile -Delimiter ';' -Header Title,FileName,Link | Group-Object Title | ForEach-Object {
  14.     # combine the Title with the source path. Remove invalid characters from the Title
  15.     $targetFolder = Join-Path -Path $sourcePath -ChildPath ($_.Name -replace $invalid)
  16.     # if the destination folder does not already exist, create it
  17.     if (!(Test-Path -Path $targetFolder -PathType Container)) {
  18.         $null = New-Item -Path $targetFolder -ItemType Directory
  19.     }
  20.     foreach ($fileName in $_.Group.FileName) {
  21.         $targetFile = Join-Path -Path $sourcePath -ChildPath $fileName
  22.         if (Test-Path -Path $targetFile -PathType Leaf) {
  23.             Move-Item -Path $targetFile -Destination $targetFolder -Force
  24.         }
  25.         else {
  26.             Write-Warning "File '$targetFile' not found!"
  27.         }
  28.     }
  29. }
Add Comment
Please, Sign In to add comment