Solved

Rename files based on their parent folder name

Posted on 2009-07-13
5
728 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect, Thank you so much...you just saved me about 3 days of work.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

9 Experts available now in Live!

Get 1:1 Help Now