For my scripts the structure of the file names doesn’t matter as long as it ends with “.pdf”
If I understand well you may use:
Script 1 to store the PDFs and possibly clear old folders
# Code Yvan/GK Script 1 --> Pdf vers Caldera
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.2"
use scripting additions
----------------------------------------------------------------
property forTests : true # during tests I use copy object so the original files remain in place
# Working Folders
set sourceDirectory to POSIX path of "Macintosh HD:Users:dupont:Desktop:Entree:"
set logDirectory to POSIX path of "Macintosh HD:Users:dupont:Desktop:Logs:"
set destinationDirectory to POSIX path of "Macintosh HD:Users:dupont:Desktop:Caldera:"
# Date used to build the name of the log file / month
set theDate to current date
set time of theDate to 0 -- for future comparisons
set dateMinus45 to theDate - 45 * days -- for future comparisons
set reportPath to logDirectory & my monthStamp(theDate)
if not (exists object reportPath) then
# Whe entered a new month, creates a new logfile
my writeto(reportPath, "", «class utf8», false)
end if
# Build a list of files embedded in sourceDirectory
set workingFiles to objects of sourceDirectory result type paths list with searching subfolders without include folders and include invisible items
repeat with aFile in workingFiles
if aFile ends with ".pdf" then
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"} # ADDED
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2" # ADDED
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
--supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest2)) < dateMinus45 then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest1)) < dateMinus45 then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
# Create the destination subfolders if they aren't available
set finalDest to create folder at (destinationDirectory & "/" & partialPath) use name lateSubFolder --> "xxx/D1/D2/D3"
# Move the file
if forTests then
copy object aFile to folder destinationDirectory new name newName
#log result
my writeto(reportPath, my horoDateur() & " - PDF, ajout correct sur RIP Caldera: " & newName & linefeed, «class utf8», true) # append data to the log file
else
move object aFile to folder finalDest new name newName
#log result
my writeto(reportPath, my horoDateur() & " - PDF, ajout correct sur RIP Caldera: " & newName & linefeed, «class utf8», true) # append data to the log file
end if
on error e number n
my writeto(reportPath, my horoDateur() & " - Le fichier: " & aFile & " n'a pas été traité, il est disponible" & linefeed, «class utf8», true) # append data to the log file
end try
end if
end repeat
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
on horoDateur()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd HH:mm:ss"
return (theFormatter's stringFromDate:(current date)) as text
end horoDateur
#=====
on monthStamp(aDate)
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy_MM"
return ("Log_" & (theFormatter's stringFromDate:aDate)) & ".txt"
end monthStamp
#=====
Script 2 for the late task:
# Code Yvan/GK Script 2 --> Caldera-ppm vers Serveur
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.2"
use scripting additions
----------------------------------------------------------------
property forTests : true # during tests I use copy object so the original files remain in place
# Working Folders
set sourceDirectory to POSIX path of "Macintosh HD:Users:dupont:Desktop:CalderaPPM:"
set logDirectory to POSIX path of "Macintosh HD:Users:dupont:Desktop:Logs:"
set destinationDirectory to POSIX path of "images:0_PPM:" -- Folder on remote server -- local folder for test "Macintosh HD:Users:dupont:Desktop:0_PPM:" --
# Date used to build the name of the log file / month
set theDate to current date
set time of theDate to 0 -- for future comparisons
set dateMinus45 to theDate - 45 * days -- for future comparisons
set reportPath to logDirectory & my monthStamp(theDate)
# Build a list of files embedded in sourceDirectory
set workingFiles to objects of sourceDirectory result type paths list without searching subfolders and include invisible items --include folders and include invisible items
repeat with aFile in workingFiles
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"} # ADDED
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2" # ADDED
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.ppm" -- ou ".pdf" if something failed before
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
--supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest2)) < dateMinus45 then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest1)) < dateMinus45 then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
if aFile ends with ".ppm" then
# Create the destination subfolders if they aren't available
set finalDest to create folder at (destinationDirectory & "/" & partialPath) --> "xxx/D1/D2/D3"
# Move the file
if forTests then
copy object aFile to folder finalDest new name ppmName
#log result
my writeto(reportPath, my horoDateur() & " - Ppm, ajout correct sur Prepstation: " & ppmName & linefeed, «class utf8», true) # Write in the log file
else
move object aFile to folder finalDest new name ppmName
#log result
my writeto(reportPath, my horoDateur() & " - Ppm, ajout correct sur Prepstation: " & ppmName & linefeed, «class utf8», true) # Write in the log file
end if
else
# The document is not a ppm so it wasn't converted.
error number 1943 # My birthYear is a good candidate for an error number
end if
on error e number n
my writeto(reportPath, my horoDateur() & " - Erreur # " & n & " Le fichier " & aFile & " n'a pas été converti." & linefeed, «class utf8», true) # Write in the log file
end try
end repeat
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
on horoDateur()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd HH:mm:ss"
return (theFormatter's stringFromDate:(current date)) as text
end horoDateur
#=====
on monthStamp(aDate)
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy_MM"
return ("Log_" & (theFormatter's stringFromDate:aDate)) & ".txt"
end monthStamp
#=====
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 8 aout 2019 18:34:48
Added code treating errors and delete old folders in script 2