Wednesday, December 13, 2017
  • Index
  •  » Automator
  •  » Rename files to a folder name, save to different folder & add prefix

#1 2014-12-17 08:36:37 pm

bagrov
Member
Registered: 2014-12-18
Posts: 11

Rename files to a folder name, save to different folder & add prefix

I really need you help and will be very grateful for it. I think this should be no problem for you.

So I have folder with different subfolders in it. In subfolders are images. What I need is:

Change each image name to it’s subfolder name + 001 and so on («1stSubfolder001, 1stSubfolder002, …», «2ndSubfolder001, 2ndSubfolder002, …»)
Move all images from all subfolders to one folder (or at least to root folder)
Add random prefix number to names.
I have script to third task:

tell application "Finder" repeat with this_item in (get items of window 1) set name of this_item to ((random number from 0 to 99999) & name of this_item) as string end repeat end tell


But it only works with opened folder on the foreground. Maybe there is a way to make one script to run it on the background?

Huge thank you in advance.

I found cool automator app on stackoverflow, but I need to correct it a little bit. http://stackoverflow.com/questions/1583 … utomator-i

This script does everything I need BUT it renames all files to ONE folder name, not each different.

Here is another applescript that does rename files to folders, but it uses 2 folders name (folder + subfolder), and I need only subfolder prefix.

set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
    tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat


(MacBook Pro Late 2013, OS X Yosemite 10.10.1)

Last edited by bagrov (2014-12-18 05:43:58 pm)

Offline

 

#2 2014-12-18 05:43:16 pm

bagrov
Member
Registered: 2014-12-18
Posts: 11

Re: Rename files to a folder name, save to different folder & add prefix

Guys, anyone?

Offline

 

#3 2014-12-19 05:53:33 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4451

Re: Rename files to a folder name, save to different folder & add prefix

Hi bagrov. Welcome to MacScripter.

Here's a script which uses the Finder. It's not particularly fast, but I think it does what you want. The random-number prefixes are included with the renaming of the files inside the subfolders, so that the renaming only needs to be done once. There's no name checking, so if you run the script on a subsequent occasion and it happens to generate a name which has already been given to a file in the root folder, it'll error out. I don't know what you want to do in such a case. Try generating another name or overwrite the existing file?

Applescript:


set rootFolder to (choose folder)

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

tell application "Finder"
   set subfolders to rootFolder's folders
   repeat with thisSubfolder in subfolders
       set subfolderName to thisSubfolder's name
       set theseFileNames to name of thisSubfolder's files
       repeat with i from 1 to (count theseFileNames)
           -- Get this file's current name.
           set thisName to item i of theseFileNames
           -- Extract the extension.
           if (thisName contains ".") then
               set extn to "." & text item -1 of thisName
           else
               set extn to ""
           end if
           -- Generate a five-digit random number as text.
           set prefix to text 2 thru 6 of ((100000 + (random number 99999)) as text)
           -- Get a three-digit postfix number based on the repeat index.
           set postfix to text 2 thru 4 of ((i + 1000) as text)
           -- Put together the new name.
           set newName to prefix & subfolderName & postfix & extn
           -- Rename the file.
           set name of file thisName of thisSubfolder to newName
       end repeat
       -- Move all the files from this subfolder to the root folder.
       move thisSubfolder's files to rootFolder
   end repeat
end tell

set AppleScript's text item delimiters to astid

beep 3 -- To signal completion.

Edit: Typo in script corrected.

Last edited by Nigel Garvey (2014-12-19 06:29:46 am)


NG

Offline

 

#4 2014-12-19 05:55:33 am

bagrov
Member
Registered: 2014-12-18
Posts: 11

Re: Rename files to a folder name, save to different folder & add prefix

Oh my god thank you very much!
You are a lifesaver!

Offline

 
  • Index
  •  » Automator
  •  » Rename files to a folder name, save to different folder & add prefix

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)