Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename files based on their parent folder name

Posted on 2009-07-13
5
Medium Priority
?
739 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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