Link to home
Start Free TrialLog in
Avatar of mrkipling
mrkipling

asked on

append jpg file names with directory names...

I currently have a lot of images that each reside in named directories, for instance

dir1 >> dir2 >> file.jpg

I need to ammend each jpg filename with the name of the containing dir names, using an underscore to seperate each dir name, so for instance, in the example above, the jpg filename would become "dir1_dir2_file.jpg" can anyone provide me with a solution of how to achieve this .

Thanks

Mark
Avatar of Dexstar
Dexstar

@mrkipling:

> I need to ammend each jpg filename with the name of the containing dir names,
> using an underscore to seperate each dir name, so for instance, in the example
> above, the jpg filename would become "dir1_dir2_file.jpg" can anyone provide
> me with a solution of how to achieve this .

I wrote a VBScript that will do it for you.  The code is listed below.  To use it, follow these steps:

1) Create a new text document in notepad, and put in the code listed below, and save it as "DirNames.vbs".
2) Change the line that says "SOURCE_PATH", and set it to be the full path of the directory with the JPEG files that you want to rename.
3) Double-Click on the File.

Your files will then be renamed according to the directory.


Hope That Helps,
Dex*

      OPTION EXPLICIT

      ' Declare Constants
      CONST SOURCE_PATH = ".\"
      CONST FILE_EXT = ".jpg"

      ' Declare variables
      Dim fso, objSourceFolder, objFile

      ' Create Objects
      Set fso = CreateObject("Scripting.FileSystemObject")

      ' Get the source folder
      Set objSourceFolder = fso.GetFolder( SOURCE_PATH )

      ' Process each File in the Folder
      For Each objFile In objSourceFolder.Files
            ' Check the Extension
            If ( Right(objFile.Name, Len(FILE_EXT)) = FILE_EXT ) Then
                  ' Append the dir to the file name
                  objFile.Name = Replace(Mid(objFile.Path, 4),"\","_")
            End If
      Next
Avatar of mrkipling

ASKER

Thanks for the help dexstar, unfortunatly i cant seem to get the script to run, i have entered the dir path as follows, is this correct ?

OPTION EXPLICIT

     ' Declare Constants
     CONST SOURCE_PATH = "C:\Temp\dir\VXK\758"
     CONST FILE_EXT = ".jpg"

The jpg file resides in the directory 758 but when i run the script nothing seems to happen.

I also need the script to be able to work through directories and name the jpgs as it goes, for instance inside dir1 i may have several dir's each with a single jpg file eg,

dir1>>dir2>>file.jpg
      >>dir3>>file.jpg
      >>dir4>>file.jpg
      >>dir5>>file.jpg

is this possible ?

Thanks again

Mark
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic, it didnt work at first, but this was because all the .jpg extensions where in uppercase as the images where originally created on a mac. changed the constant "jpg" to uppercase and its worked a charm, thanks Dexstar, Increased points to 300.

thanks

Mark
Yikes!  I didn't think about the case on the extension.

1) Make your define for "FILE_EXT" to be capitalized (like you already did.
2) Change this line:
     If ( Right(objFile.Name, Len(FILE_EXT)) = FILE_EXT ) Then

To this:
     If ( UCase(Right(objFile.Name, Len(FILE_EXT))) = FILE_EXT ) Then

Then the case won't ever matter.  Anyway, just a suggestion for the future.  Thanks for the extra points!

Dex*

PS:  Don't run it twice on the same directory, or else all your files will end up with the directory added to their name AGAIN!