Hello All,
This may be the wrong place for this and if so I apologize in advance.
I have been tasked with organizing a drive full of marketing assets on a mac server. It is currently in use so we have made a duplicate share so that any changes will not confuse the users and no more assets will be randomly added. The assets stored are in no way, shape, or form named or placed in a meanignful manner and they are also stored either in folders stored in folders stored in folders or just sitting outside of a folder. They range anywhere from jpg to psd to mov and are either named according to something involved with the project or they have a random number or letter and they either have spaces or not. I have started organanizing them by type but have yet to remove or rename anything in folders because all of these will eventually be tagged for easier usability later on. I also have duplicates to contend with (even after I have gone through and removed over 20,000). Suffice it to say, this is a tedious job and I would like to try and make it a bit less so. My question is: would it be possible to write a script that would recursively step through each folder and prepend the parent folder name to each file contained within? And if that is possible would it also be a possibility to maybe then move those files to the root or some other specified place? I have some minor experience in writing scripts but it was for a linux and windows class and did not pertain to... anything really. So currently what I have tried to write for this consists of a mashup of random crap I found with google and I'm sure would be an embarrassment if it were posted.

The file structure I am working with is similar to this:

|_ Graphics
   |_ Digital Media
      |_ 1.jpg
         |_ 50th Anniversary_Photos 2011-2012
            |_ John_Anderson.jpg
               Ron-Zink 2011.png

So being that this is just a very small example of the hell I am living in I would like to try and find a way to rename each file with the parent folder name prepended so that when I later have to go through and add tags to every file named "1.jpg" I have something to go on when it is called "50th Anniversary_Photos 2011-2012_1.jpg". I am vaguely concerned that if the script is taking the parent folder name it will also prepend the parent of the parent folder name but after exploring even further into the structure I feel this actually might be a benefit due to the fact that at some point the folders start to lose the descriptive names and fall into the random number naming scheme as well. If I have left out anything please let me know and as always, thank you for any help in advance.

Here is a simple ruby script that will (from the directory you call it in) give you all files (recursively) with full path names changing / to _.

require 'pathname'
Dir['**/*'].each { |f| puts Pathname(f).to_s.gsub('/', '_') }

So the output is something like:


You could send that output to a file as it is or change the script to actually move/copy the file to a new location with the new name. I leave that up to you.

You still will have your work cut out for you, though. What if the dirname or file name have an underscore in them? Perhaps it matters, perhaps not. Once you get this massive list of new names, what is your intent from there? Manual processing? Automated processing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.