Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "This script takes a directory path as an argument and concatenates the current date
  2.  to all jpeg file names"
  3.  
  4. [ Object < #CoralTmp ]
  5.  
  6. CoralTmp class>>usage
  7. [
  8.     Console << 'Usage: coral ' << CoralScriptWriting getScriptName << ' directory'  << String crlf.
  9. ]
  10.  
  11. [
  12.     | folder fs folderRef stream date jpegFiles |
  13.  
  14.     (CoralScriptWriting hasOption: #h) ifTrue: [ CoralTmp usage. ^ self ].
  15.  
  16.     [ folder := CoralScriptWriting getAllArgumentsWithoutMinusButScriptName first ]
  17.         on: SubscriptOutOfBounds do: [ Console << String crlf. CoralTmp usage. ^ self ].
  18.  
  19.     fs := FSDiskFilesystem currentClass createDefault.
  20.     folderRef := fs root navigateWith: (fs resolve: folder).
  21.     folderRef isDirectory ifFalse: [ Console << 'This directory does not exist.'. ^ self ].
  22.    
  23.     stream := WriteStream on: String new.
  24.     DateAndTime today printYMDOn: stream.
  25.     stream close.
  26.     date := stream contents.
  27.    
  28.     jpegFiles := folderRef children select: [ :each |
  29.         | extension |
  30.         extension := each extension asLowercase.
  31.        
  32.         #(jpg jpeg jpe jfif jif) anySatisfy: [ :ext | extension endsWith: ext ]
  33.     ].
  34.        
  35.     jpegFiles do: [ :each |
  36.         | newName |
  37.         newName := WriteStream on: String new.
  38.         newName
  39.             nextPutAll: each parent fullName;
  40.             nextPut: $/;
  41.             nextPutAll: each base;
  42.             nextPut: $_;
  43.             nextPutAll: date;
  44.             nextPut: $.;
  45.             nextPutAll: each extension.
  46.        
  47.         Console << '> Process: ' << each basename << String crlf.
  48.         each renameAs: newName contents.
  49.     ].
  50.    
  51.     Console << 'Done.' << String crlf.
  52. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement