[SOLVED] Automatically rename folder, create subfolder and move files

Hey all :>
my music directory is a bit messed up and untidy. all albums were encoded into one single folder:
$artist - $album ($year)
Aerosmith - Toys in the Attic (1975)
how can i create a parent folder with the name of the artist (here: Aerosmith), a subfolder for the album without the year (Toys in the Attic) and move all mp3's into it? i have no idea how to handle filenames with bash
thx for every comment! :>
Last edited by arch0r (2010-11-28 16:58:01)

eh, figured I'd make it a little more elegant and add some directory checks. this should work for all cases theoretically so that if you add music later on you can update your directories
preconditions: newly added directories are in the format that was prescribed by you and the output doesn't contain any dashes. (I based my glob off of the presence of a dash.)
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
chdir "music" or die "cuidado! $!";
my @directories = glob "*-*";
my @artists;
my %albumhash;
foreach(@directories)
push(@artists,split(/ - /,$_));
while(@artists)
my $key = shift(@artists);
my $value = shift(@artists);
push(@{$albumhash{$key}}, $value);
foreach(keys %albumhash)
if(!(-d $_))
mkdir $_,0755 or die "couln't makedir\n";
chdir $_;
foreach(@{$albumhash{$_}})
my $dirname = substr($_,0,(length($_) - 7));
if(!(-d $dirname))
mkdir $dirname,0755 or die "couldn't make subdir\n";
chdir "..";
foreach(@directories)
my $current = $_;
my $targetdir = substr($_,0,(index($_,'-') - 1)) . "/" . substr($_,(index($_,'-')+2),(index($_,'(') - index($_,'-') - 3));
chdir $current;
my @musicfiles = glob("*");
chdir "..";
foreach(@musicfiles)
my $old = "$current/$_";
my $new = "$targetdir/$_";
move($old,$new) or die "Danger Will Robinson! $!";
rmdir($current);
Thanks for the fun distraction, normally I don't like being someone's personal army but you caught me during my vacation   you should be receiving my bill via e-mail (just kidding):D
Last edited by Cyrusm (2010-11-28 16:01:53)

Similar Messages

  • Applescript create folder from filename and move file stopped working

    Hi,
    For a while I've used this script but after upgrade and a new machine it stopped working.
    I have  Mac OS 10.8.5.
    This is what I have.
    Images (folder)
    111111_00.jpg
    111111_01.png
    222222_01.jpg
    222222_02.png
    222222_03.gif
    I'd like to be able to create a new folder for each "group of files" (new folder 111111, new folder 222222) and move the appropriate files into each of them.
    The filenames are always built by 6 digits underscore and 2 digits. Can be whatever filetype. (123456_01.jpg)
    I have a script that used to work just fine but now it just creates one folder and then freezes.
    This is the script that doesn't work.
    set chosen_folder to (choose folder)
    tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "_"}
    tell application "Finder"
        try
            set file_list to files of chosen_folder
        on error
            set file_list to {}
        end try
        repeat with this_file in file_list
            set folder_name to name of this_file
            if folder_name contains "." then set folder_name to ((text items 1 thru -2 of folder_name) as string)
            set new_folder to ((chosen_folder as string) & folder_name & ":")
            try
                get new_folder as alias
            on error
                make new folder at chosen_folder with properties {name:folder_name}
            end try
            move this_file to folder new_folder without replacing
        end repeat
    end tell
    set my text item delimiters to old_delim
    I know that there are many who can do this easy as pie, but if you can find it in your hearts to help me it would be much appreciated.
    K

    function createFolder(file) {
      var parentFolder = file.parent;
      var saveFolder = new Folder( parentFolder + '/' + file.name.substring( 0, 8 ) );
      if( !saveFolder.exists ) saveFolder.create();
      var saveFile = new File( saveFolder + '/' + file.name);
      if( file.copy( saveFile ) ) file.remove();
    function main() {
       var folder = new Folder("~/Desktop/images");
       var files = folder.getFiles();
       for (var i = 0; i < files.length; i++) {
         var f = files[i];
         if (f instanceof File) {
          createFolder(f);
    main();
    Something like that should work.

  • Using foreach loop container with file system task to rename and move files

    the foreach loop container will not rename and move files for me
    sukai

    duplicate of
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/8f2899f1-43b0-47e0-8bd0-e082989cdcb8/file-system-task-and-foreach-loop-container?forum=sqlintegrationservices
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Using AppleScript to create folders and place files in them

    Hi guys,
    I've been reading through the forum for the better part of the last 6 hours wracking my brain trying to figure out how to accomplish what I'm trying to do. I've been playing around with Automator and also trying to decipher some Apple Script samples that were posted on here, but I'm really having no luck at all. I'm sure you've all been there where you feel like you're on an endless search to solve a problem and it feels like there's no hope. I've tried every single possible thing I can and I'm getting close, but it's never quite right.
    Here's what I'm trying to do, and hopefully some kind soul will help me out. I'm deseperately trying to figure this out, and any help at all would be GREATLY appreciated!
    I have a bunch of PDF files that I scan, in the hundreds. After I've scanned them I'm left with something like this:
    1234567_Elephant.pdf
    1234567_Duck.pdf
    1234567_Cat.pdf
    1234567_Cat_01.pdf
    1234567_Dog.pdf
    3431233_Elephant.pdf
    3431233_Dog.pdf
    3431233_Dog_01.pdf
    3431233_Duck.pdf
    etc...
    So they have a 7 digit ID, then the file name which is always one of the 4 options (I'm just using the animals as examples, but they would be other words).
    What I'd want the script to do is categorize these into folders based on the ID number, and then based on the "animal" and then the files within them. Note, some files have an _01 or _02 etc... appended to them. I don't know if this makes a difference.
    So after running the script I'd love to see
    1234567 (Folder)
         Elephant (Subfolder)
              1234567_Elephant.pdf (File)
         Duck (Subfolder)
              1234567_Duck.pdf (File)
         Cat (Subfolder)
              1234567_Cat.pdf (File)
              1234567_Cat_01.pdf (File)
         Dog (Subfolder)
              1234567_Dog.pdf (File)
    3431233
         Elephant
              3431233_Elephant.pdf
         Dog
              3431233_Dog.pdf
              3431233_Dog_01.pdf
         Duck
              3431233_Duck.pdf

    well, using a couple of stock handlers I had lying around, and making the following assumptions:
    that an underscore is always the delimiter used
    that the file name is always a single word like 'dog' or 'cat' (if you have multiple word file names, the script needs to be modified some)
    this should do what you ask:
    set mainFolder to (choose folder) as text
    tell application "System Events"
      -- get all the unsorted files, and loop through
              set unsortedFiles to every file of folder mainFolder whose visible is true
              repeat with thisFile in unsortedFiles
      -- split the file name on underscores and periods
                        set fileNameBits to my tid(name of thisFile, {"_", "."})
      -- first item of list is id, make/get correct folder
                        set IDFolder to my checkForFolder(mainFolder, item 1 of fileNameBits)
      -- second item of list is file name, make/get correct folder in id folder
                        set groupFolder to my checkForFolder(IDFolder, item 2 of fileNameBits)
      -- move file
      move thisFile to groupFolder
              end repeat
    end tell
    to checkForFolder(fParent, fName)
      -- subroutine checks for folder, creating it if it doesn't exist
              tell application "System Events"
                        if not (exists folder fName of folder fParent) then
                                  set output to path of (make new folder at end of folder fParent with properties {name:fName})
                        else
                                  set output to (path of (folder fName of folder fParent))
                        end if
              end tell
              return output
    end checkForFolder
    on tid(input, delim)
      -- subroutine for handling text item delimiters
              set {oldTID, my text item delimiters} to {my text item delimiters, delim}
              if class of input is list then
                        set output to input as text
              else
                        set output to text items of input
              end if
              set my text item delimiters to oldTID
              return output
    end tid

  • Using Automator and Applescript to search and move files to flash drive

    I've used applescript and automator to do simple tasks like watch folders and move files and rename files, but that is about the extent. I am wondering if it is possible to set up a automator service or app that will do several things.
    When a flash drive is plugged it, it will ask for a file to be searched for, then it will search a predetermined directory for files with the word or number in them and then it will copy the found files to the mounted flash drive.
    Any help would be greatly appriciated!
    Thanks!

    As a start, you can use launchd to run a script when a volume is mounted:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
              <key>Label</key>
              <string>com.tonyt.LaunchOnVolMount</string>
              <key>ProgramArguments</key>
              <array>
                        <string>LaunchOnVolMount.sh</string>
              </array>
              <key>QueueDirectories</key>
              <array/>
              <key>StartOnMount</key>
              <true/>
              <key>WatchPaths</key>
              <array/>
    </dict>
    </plist>
    You can then have the LaunchOnVolMount.sh script perform the tasks you need.
    You can incorporate Applescript within the Bash script with osascript:
    #!/bin/bash
    answer=$( osascript -e 'tell app "System Events" to display dialog "What file do you want to search for?" default answer "" ' )
    echo $answer
    I beleive that you can also call an Applescript from launchd

  • WPA8.1 Feature to save and move files between folders and OneDrive Folders

    Hi,
    I would like to suggest to dev team a new feature to Adobe Reader on Windows Phone.
    Today I was done a uggly workarround to save one PDF file on a OneDrive folder to send to my classmates. So I would like a feature to save and move files between folders on SD cards and OneDrive to keep my phone organized.
    thank you for your time.

    Here:
    property aFolder : "a"
    property bFolder : "b"
    property cFolder : "c"
    property dFolder : "d"
    property eFolder : "e"
    tell application "Finder"
    try
    set theLocation to the selection as alias
    on error
    set theLocation to (folder of the front window as alias)
    end try
    if not (exists folder aFolder of theLocation) then make new folder at theLocation with properties {name:aFolder}
    if not (exists folder bFolder of theLocation) then make new folder at theLocation with properties {name:bFolder}
    if not (exists folder cFolder of theLocation) then make new folder at theLocation with properties {name:cFolder}
    if not (exists folder dFolder of theLocation) then make new folder at theLocation with properties {name:dFolder} 
    if not (exists folder eFolder of theLocation) then make new folder at theLocation with properties {name:eFolder}
    set this_list to every file of theLocation
    repeat with i in this_list
    if (name of i) begins with "a" then
    move i to folder "a" of theLocation
    else if (name of i) begins with "b" then
    move i to folder "b" of theLocation
    else if (name of i) begins with "c" then
    move i to folder "c" of theLocation
    else if (name of i) begins with "d" then
    move i to folder "d" of theLocation
    else if (name of i) begins with "e" then
    move i to folder "e" of theLocation
    end if
    set name of (the result) to items 3 thru -1 of (get name of (the result)) as string
    end repeat
    end tell
    (98558)

  • I want to transfer my photo and movie files from my iPhone 4S to my mac.

    I have posted this question before without any success and was hoping by reposting it may promt a solution to my problem.
    I want to transfer my photo and movie files from my iPhone to my Mac OS X (10.5.8).
    But I don't want to use Cloud or iTunes.
    Is there a way to connect my iPhone 4S to my Mac directly,
    say ... via a program or such on my MAC, to drag and drop the photos and Movie files,
    directly into a folder. ??????????
    Reason want to connect iPone to MAC or MAC to iPhone is ....
    I have no DATA on my iPhone Mobile plan at moment.
    So Photosync app is no good, as I can't download the app to my iPhone.
    I can download a Photosync to my MAC ...
    but with the Photosync app you can only send data.
    Thanks in advance for any help.

    Sorry I should of mentioned ....
    Reason want to connect iPone to MAC or MAC to iPhone is ....
    I have no DATA on my iPhone plan at moment.
    So Photosync app is no good, as I can't download the app to my iPhone.
    I can download a Photosync to my MAC ...
    but with the Photosync app you can only send data  (so needs to be on the  iPhone).

  • How to create parameter and control file like filename + date

    Hello there
    I am trying to create parameter and control file with following command
    in SQLPLUS
    create pfile='/u03/oradata/WEBDB/backup/initWEBDB.ora' from spfile;
    In RMAN
    copy current controlfile to '/u03/oradata/WEBDB/backup/cf_longterm.cpy';
    how can I put date at the end of filename like
    initWEBDB8jan06.ora and cf_longterm8jan06.cpy
    Thanks in advance
    Lionel

    ASM is reliable but a smart DBA is very careful. If ASM is doing mirroring this is like RAID doing mirroring. What happens if you accidentally delete one copy ... the other one disappears instantly. Not a good idea.
    With respect to redo logs you need a minimum of three groups, two members, and one thread per instance. So a 2 node cluster should, at a minimum have 12 physical files.
    Not mirroring the redo logs, assuming multiple members, is not as critical.

  • .mp4 and .mov files are just LARGE

    Sorry, without posting all the encoding details, is there an obvious reason why my .mp4 and .mov files are ~10x the size that they should be after AME? I'm talking about files that are 30mb .mp4's before and 300mb.mov afterwards. If you need details I'll post. Thanks.

    File size is determined primarily by only two factors - duration and bitrate.  The codec used, resolution, frame rate, etc. all have little or no effect on file size.
    Duration, and bitrate.  If you want a smaller file, one or the other of those has to be reduced.

  • Export current format - JPG and MOV files

    When exporting jpg and mov files in "current format" the mov files will be exported with only 1 second (ca 130kb).
    If I export in "original format" it exports the mov files as they are, but the pictures are "original" and not show the last version.
    Any idea?

    If you dleted them you'll need to use a file recovery application like File Salvage
    Regards
    TD

  • Hi, I have downloaded some avi files and mov files. i am not getting audio while playing the movie. i tried to play with iTunes, QT, DivX player. but I am getting audio in mp4 files. can u help me to fix this

    Hi, I have downloaded some avi files and mov files. i am not getting audio while playing the movie. i tried to play with iTunes, QT, DivX player. but I am getting audio in mp4 files. can u help me to fix this

    First, back up all data and then uninstall "DivX" according to the developer's instructions.
    Then try this:
    VideoLAN - Download official VLC media player for Mac OS X

  • Create folder and move files to specific directory in AL11

    My requirement is to create a new folder in one of the existing directories in AL11 and move some files into it.
    I searched the forum and found this solution but it creates folder only in DIR_HOME directory.
    data: unixcom like rlgrap-filename.
    data: begin of tabl occurs 500,
    line(400),
    end of tabl.
    unixcom = 'mkdir mydir'. "command to create dir
    "to execute the unix command
    call 'SYSTEM' id 'COMMAND' field unixcom
    id 'TAB' field tabl[].
    I want the folder to be created in some other directory DIR_INTERFACES .
    Any suggestions are welcome ..

    Get complete path (physical path) using FM FILE_GET_NAME_USING_PATH.
    Default directory is DIR_HOME, hence it creates file in that directory...If you specify complete folder name, it will be created under that path..
    Concatenate mkdir
    space
    <physical path>
    mydir
    into unixcom.

  • Batch create folder and move files

    Hi,
    I have files such as:
    FD3_xxx1
    FD3_xx2
    Fd3_xxx3
    FD63_xxx1
    FD63_xx2
    Fd63_xxx3
    How would I create a batch file that would create folders with the characters before the "underscore" and then add the files into them. For example, all "FD3" files will go in a folder called "FD3" and all "FD63" files will go into a folder called "FD63".

    Dear Frederik and/or Bill,
    I have come across this question and your answer and it seems that this is a solution to may problem as well. However, you say that a user needs "to activate it to do something useful". I have no experience in programming and I do not know how to activate
    it. Please let me what to do to make this work.
    One needs to be careful when running batch files that move or delete large numbers of files in one fell swoop. This is why I posted the file in a passive form. To be on the safe side you must do this:
    Open a Console session with cmd.exe.
    Invoke the batch file in its current (=passive) form.
    Carefully examine the screen output it generates.
    If the output fully agrees with your expectations, activate the batch file by modifying this line
    echo move %1 %Folder%
    to read like so:
    move %1 %Folder%
    Why? Because the batch file command "echo" will write things to the screen but do nothing else. Hence the command
    echo Hell World!
    will put the phrase "Hello World!" on the console screen. Once you remove the word "echo", you will actually execute the "move" command:
    move %1 %Folder%
    A general observation: I feel somewhat uneasy about you running a batch file that includes advanced commands, apparently without you really knowing what it is doing and how it is doing it. Performing the task manually might be a safer option.

  • Droplet to Rename and Move files into Folders

    Hello,
    I am a Graphic Designer and I am trying to optomize my workflow. I would like to be able to save out files with different prefixes in a folder then run a droplet on the folder that will create sub-folders and move the files to their respective folders based on their prefix. I would also like to remove the prefix from the filename after it is moved.
    Here is an example of what I am trying to achieve:
    So far I am able to Create the folders, and move the files, but I am stuck on removing the prefixes from the filename.
    property aFolder : "a"
    property bFolder : "b"
    property cFolder : "c"
    property dFolder : "d"
    property eFolder : "e"
    try
              tell application "Finder" to set theLocation to the selection as alias
              tell application "Finder" to set thePath to the selection as text
    on error
              tell application "Finder" to set theLocation to (folder of the front window as alias)
    end try
    tell application "Finder"
              if not (exists folder aFolder of theLocation) then
      make new folder at theLocation with properties {name:aFolder}
              end if
              if not (exists folder bFolder of theLocation) then
      make new folder at theLocation with properties {name:bFolder}
              end if
              if not (exists folder cFolder of theLocation) then
      make new folder at theLocation with properties {name:cFolder}
              end if
              if not (exists folder dFolder of theLocation) then
      make new folder at theLocation with properties {name:dFolder}
              end if
              if not (exists folder eFolder of theLocation) then
      make new folder at theLocation with properties {name:eFolder}
              end if
    end tell
    tell application "Finder"
              set this_list to every file of theLocation
              repeat with i in this_list
                        if (name of i) begins with "a" then
                                  move i to thePath & "a:"
                        else if (name of i) begins with "b" then
                                  move i to thePath & "b:"
                        else if (name of i) begins with "c" then
                                  move i to thePath & "c:"
                        else if (name of i) begins with "d" then
                                  move i to thePath & "d:"
                        else if (name of i) begins with "e" then
                                  move i to thePath & "e:"
                        end if
              end repeat
    end tell

    Here:
    property aFolder : "a"
    property bFolder : "b"
    property cFolder : "c"
    property dFolder : "d"
    property eFolder : "e"
    tell application "Finder"
    try
    set theLocation to the selection as alias
    on error
    set theLocation to (folder of the front window as alias)
    end try
    if not (exists folder aFolder of theLocation) then make new folder at theLocation with properties {name:aFolder}
    if not (exists folder bFolder of theLocation) then make new folder at theLocation with properties {name:bFolder}
    if not (exists folder cFolder of theLocation) then make new folder at theLocation with properties {name:cFolder}
    if not (exists folder dFolder of theLocation) then make new folder at theLocation with properties {name:dFolder} 
    if not (exists folder eFolder of theLocation) then make new folder at theLocation with properties {name:eFolder}
    set this_list to every file of theLocation
    repeat with i in this_list
    if (name of i) begins with "a" then
    move i to folder "a" of theLocation
    else if (name of i) begins with "b" then
    move i to folder "b" of theLocation
    else if (name of i) begins with "c" then
    move i to folder "c" of theLocation
    else if (name of i) begins with "d" then
    move i to folder "d" of theLocation
    else if (name of i) begins with "e" then
    move i to folder "e" of theLocation
    end if
    set name of (the result) to items 3 thru -1 of (get name of (the result)) as string
    end repeat
    end tell
    (98558)

  • I have 2 auxillary disk drives for my MacBokk Pro. Time Machine uses one for backup and I use the other for storage, but I am unable to create a new folder or drag and drop files or folders to the aux drive.

    I am unable to create a new folder on either of my auxillary disk drives.  I can access the files and folders that were there when I transferred one of the drives from a PC.  Time machine uses one for backup and I want to use the other to store photos which are large files.  An help would be appreciated.

    Thanks for the reply.  If I reformat the drive will I be able to access the files on it from the MAC.  I do not share the drive with a PC.  The files were originally created on a PC which I no longer use.  I do want to continue to access the files put there by the PC.  I would like to not have to copy them over to the MAC hard drive just to use them.  The second aux drive is new and works fine because it was formatted by the MAC.  I can drag and drop files there and work with just as if they were on the internal drive.

Maybe you are looking for

  • New External Monitor for Logic on MacBook Pro - Suggestions

    I'm in the painful process of updating my studio and now my large screen external monitor has started failing. I know if this a pretty general question more suited to the MacBook Pro forum but I'd like some input from folks used to working with an ap

  • Install cache error

    I'm trying to install itunes 10.6 on windows 7 home premium.  it downloads the software and starts to install.  I receive an error:  "an error occurred while attempting to create the directory c:/program data/apple/installer cache" I click retry seve

  • SAP Memory, Set and Get parameter

    Hi All,      I am running two programs in background from program1 Eq: Program1 --> Calls 2 programs                           Program1_01.(First Program)                           Program2_01(Second Program). Programs1 schedules Program1_01 and Prog

  • I reset my itouch and it got reset as an ipod.  How do I get it back?

    I recently tried to reset my itouch to the factory specs and screwed it up.  It is now just an ipod and I need to know how to get it back.  Can you help?

  • Swf file exported from Captivate 2 should pause at each slide, but doesn't always

    I have made a tutorial in Captivate 2 and exported it to an swf format. In Captivate, I had put a button at the end of each slide to pause it. I have gone back and checked to see if each slide was correctly paused and it was. When I play it in Flash