Solved

Rename files based on their parent folder name

Posted on 2009-07-13
5
732 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

837 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