Sunday, June 16, 2019

#1 2019-05-23 05:24:36 pm

AndyDesign
Member
Registered: 2019-05-23
Posts: 1

Automate moving and renaming files

I have files nested within multiple sub-folders. I want to move these files, one folder up the folder structure and rename the files as per its new parent folder. I will need this to be automated with a script.

Example logic
1. Find Root folder
2. Get contents of Subfolder of Subfolder files.
3. Move files to its Parent folder (aka 'Attachments' folder)
4. Rename files to with its current Parent folder name.
5. Delete empty Subfolder
6. Loop above action of Root folder folders until complete.

Example folder structure
Example folder structure

Desired Result
Desired Result


Filed under: Automator, AppleSrcipt

Offline

 

#2 2019-05-27 11:05:48 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 145

Re: Automate moving and renaming files

AndyDesign wrote:

I have files nested within multiple sub-folders. I want to move these files, one folder up the folder structure and rename the files as per its new parent folder. I will need this to be automated with a script.

Example logic
1. Find Root folder
2. Get contents of Subfolder of Subfolder files.
3. Move files to its Parent folder (aka 'Attachments' folder)
4. Rename files to with its current Parent folder name.
5. Delete empty Subfolder
6. Loop above action of Root folder folders until complete.

Example folder structure
2UeXG.png

Desired Result
2UeXF.png



DANGEROUS SCRIPT! MAKE FIRST BACKUP OF ROOT FOLDER (HERE IS "APPLICATIONS" FOLDER):

Applescript:


set rootFolder to path to applications folder
tell application "Finder" to set theSubfolders to every folder of rootFolder

repeat with theItem in theSubfolders
   tell application "Finder"
       set theSubfolder2 to folder 1 of theItem
       set theFile to file 1 of theSubfolder2
   end tell
   set name of theFile to (name of theItem)
   tell application "System Events"
       move theFile to theItem
       delete theSubfolder2
   end tell
end repeat

Last edited by KniazidisR (2019-05-30 11:02:40 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)