Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. PicturesLandscape,landscape
  2. PicturesBatman,batman
  3.  
  4. $picture_database_location = "$PSScriptRootpictures_database.txt"
  5. $filter_ext = "jpg"
  6. $rootDestination = "R:"
  7.  
  8.  
  9. function determineIfFileorDirectoryExists
  10. { Param([string]$fileordirectory)
  11.  
  12. <#
  13. Determine if a File or Directory Exists. If the file or directory doesn't exists,
  14. it will throw an exception.
  15.  
  16. Args:
  17. string: $fileordirectory - the variable that contains the file or directory
  18. we're going to Test.
  19.  
  20. Returns:
  21. None
  22. #>
  23.  
  24. $fileordirectoryExists = Test-Path $fileordirectory
  25.  
  26. if($fileordirectoryExists -eq $false){
  27. throw [System.IO.DirectoryNotFoundException] "Not Found: $fileordirectory"
  28. }
  29. }
  30.  
  31. function determineargumentCount
  32. { Param([string[]]$arraytoverify)
  33.  
  34. <#
  35. Determines if the array length is correct. If not, it will throw and display the elements in the array.
  36.  
  37. Args:
  38. string[]: $arraytoverify - the array to check for length.
  39.  
  40. Return:
  41. None
  42. #>
  43.  
  44. if ($arraytoverify.Length -ne 2){
  45. throw "Argument count incorrect. # of Parameters: $argumentCount. `nProvided Parameters: $arraytoverify"
  46. }
  47. }
  48.  
  49.  
  50. try{
  51.  
  52. # Before renaming any images we need to verify that the source location and destination exist.
  53. determineIfFileorDirectoryExists -fileordirectory "$picture_database_location"
  54. determineIfFileorDirectoryExists -fileordirectory "$rootDestination"
  55. $picture_database = Get-Content $picture_database_location
  56.  
  57. ForEach ($image in $picture_database){
  58. # Construct an array based on the split string
  59. # $imagaeArray is constructed as follows:
  60. # $imageArray[0]: The directory that contains the images to rename
  61. # $imageArray[1]: The base name to rename the images
  62. $imageArray = $image.Split(",")
  63. determineargumentCount -arraytoverify $imageArray
  64. # Construct the Source path with USERPROFILE environment variable and the first element in the array (the directory that contains our pictures)
  65. $imagePath = Join-Path $env:USERPROFILE -ChildPath $imageArray[0]
  66. determineIfFileorDirectoryExists -fileordirectory "$imagePath"
  67. Set-Location -Path "$imagePath"
  68. # Filter images by type and bulid a list with all the image names
  69. $fileList = (Get-ChildItem $imagePath -Filter "*.jpg").Name
  70.  
  71. if($fileList.Length -gt 0){
  72.  
  73. # Construct a string representing the desination path
  74. $DestinationPath = Join-Path $rootDestination -ChildPath $imageArray[0]
  75. $doesDirectoryExist = Test-Path $DestinationPath
  76.  
  77. if($doesDirectoryExist -eq $false){
  78. # - ErrorAction Stop - stops the script if the action take fails.
  79. md "$DestinationPath" -ErrorAction Stop
  80. determineIfFileorDirectoryExists -fileordirectory "$DestinationPath"
  81. }
  82.  
  83. # Get the number of pictures in the destionation and set the counter.
  84. [int]$fileCounter = (Get-ChildItem $DestinationPath -Filter "*.jpg").Length
  85. ForEach ($imagetoRename in $fileList){
  86. $fileCounter++
  87. $renamed_file = "{0}_{1}.{2}" -f $imageArray[1],$fileCounter,$filter_ext
  88. $imageDestination = Join-Path $DestinationPath -ChildPath $renamed_file
  89. Write-Output "Image : {0} will be renamed to {1}" -f $imagetoRename, $imageDestination
  90. Move-Item -Path "$imagetoRename" -Destination "$imageDestination"
  91. }
  92.  
  93. }
  94. }
  95. Set-Location $PSScriptRoot
  96. }
  97.  
  98. catch [System.IO.DirectoryNotFoundException]
  99. {
  100. Write-Output $PSItem.Exception.Message
  101. }
  102.  
  103. catch [System.IO.IOException]
  104. {
  105. Write-Output $PSItem.Exception.Message
  106. }
  107.  
  108. catch{
  109. Write-Output $PSItem.Exception.Message
  110. }
  111.  
  112. finally{
  113.  
  114. exit
  115. }
  116.  
  117. landscape_1.jpg
  118. landscape_2.jpg
  119.  
  120. batman_1.jpg
  121. batman_2.jpg
Add Comment
Please, Sign In to add comment