Solved

Rename files based on their parent folder name

Posted on 2009-07-13
5
730 Views
Last Modified: 2012-05-07
Ok, so hopefully this will be a simple one for one of you scripting folks, here's the situation:

There are several hundred folders, each containing multiple files (.pdf's FWIW).
These folders have an Id number as their last 2 or 3 characters (they are all in the format of <text> # <ID Number>).
The task at hand is to append all of the files within each folder so that the file name has the ID Number of the folder at the end of it, eg, XYZ.pdf is in folder "ABC # 123". XYZ.pdf should become XYZ.pdf_123.

Please let me know if i need to clarify anything.

TIA!

0
Comment
Question by:CorruptedLogic
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:TakedaT
ID: 24843366
Ive done something similar to this in the past.  It proved to be a bit difficult as it needed to have a recursive search.  As the file names got longer, a lot of the files would not stay below the character limit of a file in that particular filesystem.  How many subdirectory levels are you working with if any at all?
0
 
LVL 7

Author Comment

by:CorruptedLogic
ID: 24843625
The directory structure looks like this:

c:\docs\<text> # <123>\XYZ.pdf
c:\docs\<text> # <234>\ABC.pdf
etc etc

there are multiple PDF files in the folders but only really one sub level of folder..is that confusing? I dont feel like im explaining it very well...Hopefully a screen shot might help...

folder-structure.jpg
0
 
LVL 7

Author Comment

by:CorruptedLogic
ID: 24843648
I also just noticed that in my original post, i said the resultant file name needed to be XYZ.pdf_123, I should correct that to be XYZ_123.pdf.
0
 
LVL 10

Accepted Solution

by:
TakedaT earned 500 total points
ID: 24843771
Give this a go.  Change the first line to whatever your path is.  It will take any characters after a # symbol in the parent directory and insert it into any files in the subdirs.

Note: This will change ALL file names, not just pdfs.  If you would like me to mod it to change just pdfs, just let me know.
strSourceDir = "D:\test\test\"

dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")

dim objDir : set objDir = objFSO.GetFolder(strSourceDir)

 

SearchDir objDir

Function SearchDir(strCurrentDir)

	For Each file In strCurrentDir.Files

		file.name = left(file.name,len(file.name)-4)&"_"&right(file.parentfolder,len(file.parentfolder)-instrRev(file.parentfolder,"#"))&right(file.name,4)

	Next

	For Each folder In strCurrentDir.SubFolders

		SearchDir folder

	Next

End Function

Open in new window

0
 
LVL 7

Author Closing Comment

by:CorruptedLogic
ID: 31602974
Perfect, Thank you so much...you just saved me about 3 days of work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Macro to combine cells if duplicate 2 35
Removing first 4 characters from date field in csv file 7 33
Messed up my code 6 34
OID and Active Directory Attributes 9 57
Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now