Move a file to external disk and remove the original

This is a script that was supplied to me on these forums it has been working well, but all the transferring of the files took place locally so a ‘Move’ command does it eactly what it says, however I now need to move it to an external drive, as a result it leaves trace of the original file.

How can I make it MOVE and remove the origial File please?

set fileMatrix to {¬
          {folderName:"BHS", prefixes:{"BH70", "SM70", "AL70", "BH11"}}, ¬
          {folderName:"Bu", prefixes:{"BU40", "BU11"}}, ¬
          {folderName:"Ca", prefixes:{"CW"}}, ¬
          {folderName:"Da", prefixes:{"ES20", "AM20"}}, ¬
          {folderName:"Di", prefixes:{"DV25", "DV11"}}, ¬
          {folderName:"Do", prefixes:{"DJ30", "RA30", "DJ11"}}, ¬
          {folderName:"In", prefixes:{"GT55", "CC55"}}, ¬
          {folderName:"Fr", prefixes:{"FR10", "FR11", "FR17"}}, ¬
          {folderName:"No", prefixes:{"TM17", "NN_"}}, ¬
          {folderName:"Ma", prefixes:{"MA65", "MF65", "FI65", "MC65", "MH65", "MB65"}}, ¬
          {folderName:"Pr", prefixes:{"PR_"}}, ¬
          {folderName:"To", prefixes:{"TM15", "TM11"}}, ¬
          {folderName:"Wa", prefixes:{"WA35", "WA11"}}, ¬
          {folderName:"Se", prefixes:{"SE_"}}, ¬
          {folderName:"Mik", prefixes:{""}}}
 
tell application "Finder"
  --set theHotFolder to ((path to pictures folder) as text) & "HotFolderDone"
          set theHotFolder to folder "StudioA:Users:StudioA:Pictures:HotFolderDone"
  --set foldericon to ((path to pictures folder) as text) & "Icons:Rejected Folder Done"
          set foldericon to folder "StudioA:Users:StudioA:Pictures:Icons:Rejected Folder Done"
  --set fakeTrash to ((path to home folder) as text) & "FakeTrash"
          set fakeTrash to folder "StudioA:Users:StudioA:FakeTrash"
 
set a to alias "Scratches:Workings"
 
 
          repeat with matrixItem in fileMatrix -- look for folder
 
set destinationFolder to (folders of a whose name starts with folderName of matrixItem)
 
                    if destinationFolder is not {} then -- found one
                              set destinationFolder to first item of destinationFolder -- only one destination
                              set theFolderName to name of destinationFolder
 
                              repeat with aPrefix in prefixes of matrixItem -- look for files
                                        repeat with startFile in ((files of theHotFolder whose name starts with aPrefix) as alias list) -- move files
                                                  try
  move startFile to destinationFolder
                                                  on error
  activate
                                                            display dialog "File "" & (name of startFile) & "" already exists in folder "" & theFolderName & "". Do you want to replace it?" buttons {"Don't replace", "Replace"} default button 2 with icon 1
                                                            if (button returned of the result) is "Replace" then
                                                                      set {fileName, t_ext} to {name, name extension} of startFile
                                                                      if exists file fileName of fakeTrash then -- and it also exists in the fakeTrash Folder.
                                                                                set name of file fileName of destinationFolder to "this is a unique name" -- or whatever
                                                                                set newName to my getUniqueName(fileName, t_ext, fakeTrash)
                                                                                set movedFile to move file "this is a unique name" of destinationFolder to fakeTrash
                                                                                set name of movedFile to newName
                                                                      else -- not exists in the fakeTrash Folder.
  move file fileName of destinationFolder to fakeTrash
                                                                      end if
  move startFile to destinationFolder
 
                                                            else -- "Don't replace"
                                                                      if not (exists folder "StudioA:Users:StudioA:Desktop:Rejected Folder Done") then
                                                                                set theLastFolder to duplicate foldericon to desktop
                                                                      else
                                                                                set theLastFolder to folder "StudioA:Users:StudioA:Desktop:Rejected Folder Done"
                                                                      end if
                                                                      delay 0.5
  move startFile to theLastFolder with replacing
                                                            end if
                                                  end try
                                        end repeat
                              end repeat
                    end if
          end repeat
          try
                    if (count theLastFolder) is 0 then delete theLastFolder
          end try
end tell
 
to getUniqueName(theName, theExtension, someFolder)
  (*
     check if someFile exists in someFolder, creating a new unique file name (if needed) by adding a suffix
          parameters -          theName, theExtension : a file name and his name extension
                              someFolder : a folder (finder item or alias)
          returns :     a unique name
     *)
          set {counter, suffixes, divider} to {0, "abcdefghijklmnopqrstuvwxyz", "_"}
          if theExtension is not "" then set theExtension to "." & theExtension
          set theName to text 1 thru -((length of theExtension) + 1) of theName -- just the name part
 
          set newName to theName & theExtension
          tell application "Finder" to set theseNames to name of items of someFolder
          repeat while theseNames contains newName
                    set counter to counter + 1 -- hopefully there aren't more than 26 duplicates (numbers are easier)
                    set newName to theName & divider & (item counter of suffixes) & theExtension
          end repeat
          return newName
end getUniqueName

As I wrote quite often, I hate the Finder so I don’t use it in scripts.

Here is a sample script :


set sourceFolder to path to desktop as text
set destFolder to "target Device:target folder:"

set sourceFile to sourceFolder & "file name.txt"

do shell script "mv " & quoted form of POSIX path of sourceFile & " " & quoted form of POSIX path of destFolder

That’s all.
Of course, this is a bare code.
I don’t check that the device is available and no more checks that there isn’t already a file with the same name in the target location.

KOENIG Yvan (VALLAURIS, France) vendredi 17 mai 2013 17:00:05