Solved

Rename files based on their parent folder name

Posted on 2009-07-13
5
734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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