Saturday, January 19, 2019

#1 2010-02-14 06:47:09 pm

dminehart
Member
Registered: 2006-03-13
Posts: 4

Rename file based on folder name

I have a folder containing folders, each of which has a few files in it, one file having a specific extension, say "ext".

E.g.:

TopFolder

   Folder1
      file.ext
      file.abc
      file.xyz

   Folder2
      file2.ext
      file2.abc
      file2.xyz

What I want is to name the .ext file after its containing folder, move that newly-renamed .ext file out of its containing folder into the folder containing that folder, and delete the folder left behind. I'd be left with:

TopFolder

    Folder1.ext
    Folder2.ext


Here's what I've tried (using "testfolder" as the folder containing my folders of files):

tell application "Finder"
    set theFolders to get (every folder of ((choose folder) as string))
    repeat with aFolder in theFolders
        set Base_Name to my aFolder
        set thefiles to {get (every file in folder aFolder whose name extension is "flv")}
        repeat with aFile in thefiles
            set aFile's name to (Base_Name & "." & (aFile's name extension))
        end repeat
    end repeat
end tell


The result is an error message saying "Can’t get every folder of "Macintosh_HD:testfolder:"

Ideas, anyone?

Offline

 

#2 2010-02-15 03:24:47 am

retepp
Member
From:: Netherlands
Registered: 2007-03-29
Posts: 206

Re: Rename file based on folder name

Hi,
You could use this:

Applescript:

set mainFolder to (choose folder)
tell application "Finder"
   set subFolders to every folder of entire contents of mainFolder as string
end tell

--Peter

Offline

 

#3 2010-02-15 04:07:49 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11539
Website

Re: Rename file based on folder name

Hi,

try this, it uses a shell command to filter the files, change the literal string in the property line to your desired extension

Applescript:


property ext : "txt"

set baseFolder to POSIX path of (choose folder)
set theFiles to paragraphs of (do shell script "/usr/bin/find " & quoted form of baseFolder & " -type f -name '*." & ext & "'")
repeat with oneFile in theFiles
   set aFile to POSIX file oneFile as alias
   tell application "Finder"
       set c to name of container of aFile
       set name of aFile to c & "." & ext
   end tell
end repeat


regards

Stefan

Offline

 

#4 2010-02-15 04:16:16 am

retepp
Member
From:: Netherlands
Registered: 2007-03-29
Posts: 206

Re: Rename file based on folder name

Hi Stefan,
Nice way of doing this.
--Peter

Offline

 

#5 2010-02-15 06:12:22 am

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

Re: Rename file based on folder name

dminehart wrote:

tell application "Finder"
    set theFolders to get (every folder of ((choose folder) as string))
    repeat with aFolder in theFolders
        set Base_Name to my aFolder
        set thefiles to {get (every file in folder aFolder whose name extension is "flv")}
        repeat with aFile in thefiles
            set aFile's name to (Base_Name & "." & (aFile's name extension))
        end repeat
    end repeat
end tell


The result is an error message saying "Can’t get every folder of "Macintosh_HD:testfolder:"

Ideas, anyone?


Stefan's already given you a good alternative, but on an educational note, the error message is because you've coerced the '(choose folder)' result to string, so the line's trying to get the folders of a string instead of the folders of a folder. It should just be:

Applescript:

set theFolders to every folder of (choose folder)

After that, there are more errors waiting to kick in. The script you were trying to write should have looked something like this (tested in OSX 10.6.2):

Applescript:

set targetExtension to ".ext" -- or whatever you need it to be.
set baseFolder to (choose folder)

tell application "Finder"
   set theFolders to every folder of baseFolder
   repeat with aFolder in theFolders
       set Base_Name to name of aFolder
       -- You say only one file in the folder has the target extension.
       -- (… 'whose name extension is "ext"' doesn't work on my machine, for some reason.)
       set theFile to (first file in aFolder whose name ends with targetExtension)
       -- The Finder returns name references, so here the file's moved before renaming it.
       -- 'move' returns a reference to the file in the new location.
       move theFile to baseFolder
       set result's name to (Base_Name & targetExtension)
       
       (* Alternatively, instead of the last two lines:
       set newName to Base_Name & targetExtension
       set theFile's name to newName
       move file newName of aFolder to baseFolder *)

   end repeat
   
   delete theFolders
end tell


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)