Avatar of realParker
realParker
 asked on

read rename file automatically

Hello All,

I have a few files I want to rename automatically using data found in the file. Basically it is a ton of invoices I have scanned. I want to find the keyword "invoice: " then grab the next 10 characters that follow that keyword and rename the file with those 10 characters.

So if I have a file named abc.txt with invoice: 1234567890 in it, it will then be renamed 1234567890.txt
Scripting Languages

Avatar of undefined
Last Comment
realParker

8/22/2022 - Mon
Shift-3

Paste the script below into a text file with a .vbs extension.  Customize the value of the strFile variable with the location of the text file to be renamed.  Running the script will rename the file based on the invoice number.


Const ForReading = 1
Const TriStateUseDefault = -2
 
strFile = "c:\abc.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
strText = objTextFile.ReadAll
objTextFile.Close
 
intPos = InStr(strText, "invoice: ") + 9
strName = Mid(strText, intPos, 10)
 
strParent = objFSO.GetParentFolderName(strFile)
strExt = objFSO.GetExtensionName(strFile)
 
objFSO.MoveFile strFile, strParent & "\" & strName & "." & strExt

Open in new window

realParker

ASKER
That is just the ticket... almost. Is there a way to add a wild card or tell it to do an entire folder full of file rather than one at a time? I have about 500 files and it will be almost the same amount of work to change the strFile variable as to just change the file name myself.

Much thanks for the help so far.
ASKER CERTIFIED SOLUTION
Shift-3

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
realParker

ASKER
Thanks again for the help enjoy the points.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes