?
Solved

VBScript File Rename

Posted on 2010-11-17
14
Medium Priority
?
848 Views
Last Modified: 2012-05-10
I have a folder with many text files in it.  Each file contains the following 7 lines:

CHART #:  3563A
PATIENT NAME:  Joan Q Public
DOB:  01/18/1934
SEX:  female
SSN:  123-45-6789
NOTE DATE:  09/26/2007
Note for Joan Smith on 9/26/2007 - Chart 3563A

The File names contain a sequence number followed by the value in CHART #:  I need to renname each file using a vbscript by appending the line starting with NOTE DATE to the current file name.

The above file would be named 1234_3563A NOTE DATE: 09/26/2007.txt
0
Comment
Question by:vpnsol123
[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
  • 6
  • 3
14 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34161259
How about something like this?

~bp

@echo off
cd c:\basedir
for %%A in (*.txt) do (
  for /F "tokens=*" %%B in ('findstr /I "note date:" "%%A"') do (
    ren "%%A" "%%~nA %%B.txt"
  )
)

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34161718
You could try this too.  You can't have the colon in the file name, so I've removed that when changing it.

Try it on a test folder first.

Regards,

Rob.
strFolder = "C:\Files"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each objFile In objFSO.GetFolder(strFolder).Files
	If Right(LCase(objFile.Name), 4) = ".txt" Then
		Set objText = objFSO.OpenTextFile(objFile.Path, intForReading, False)
		strAppend = ""
		While Not objText.AtEndOfStream
			strLine = objText.ReadLine
			If InStr(strLine, "NOTE DATE:") > 0 Then strAppend = strLine
		Wend
		objText.Close
		If strAppend <> "" Then objFile.Name = Left(objFile.Name, Len(objFile.Name) - 4) & " " & Replace(strAppend, ":", "") & ".txt"
	End If
Next
MsgBox "Done"

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34163435
Good point on the colon, missed the forest for the trees I guess :-).  Anyway, in the BAT approach, we could remove the colon as follows.

~bp

@echo off
setlocal EnableDelayedExpansion
cd c:\basedir
for %%A in (*.txt) do (
  for /F "tokens=*" %%B in ('findstr /I "note date:" "%%A"') do (
    set NewName=%%~nA %%B.txt
    ren "%%A" "!NewName::=!"
  )
)

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vpnsol123
ID: 34164741
When I run this, I get invalid procedure call or agrument  line 13 character 27 error 800A0005
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34166669
Try changing this:

If strAppend <> "" Then objFile.Name = Left(objFile.Name, Len(objFile.Name) - 4) & " " & Replace(strAppend, ":", "") & ".txt"

to this:

If strAppend <> "" Then objFile.Name = Left(objFile.Name, Len(objFile.Name) - 4) & " " & Replace(strAppend, ":", "") & ".txt" End If

~bp
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 600 total points
ID: 34168681
In my code, that could be due to the fact that setting objFile.Name directly might not work.  Try this.

Regards,

Rob.
strFolder = "C:\Files"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each objFile In objFSO.GetFolder(strFolder).Files
	If Right(LCase(objFile.Name), 4) = ".txt" Then
		Set objText = objFSO.OpenTextFile(objFile.Path, intForReading, False)
		strAppend = ""
		While Not objText.AtEndOfStream
			strLine = objText.ReadLine
			If InStr(strLine, "NOTE DATE:") > 0 Then strAppend = strLine
		Wend
		objText.Close
		If strAppend <> "" Then
			strNewName = Left(objFile.Name, Len(objFile.Name) - 4) & " " & Replace(strAppend, ":", "") & ".txt"
			If objFSO.FileExists(strNewName) = True Then objFSO.DeleteFile strNewName, True
			objFSO.MoveFile objFile.Path, strNewName
		End If
	End If
Next
MsgBox "Done"

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34312869
The poster has asked to close the question after some potentially useful answers were provided, but without giving an explanation.  Not sure what "gh" represents?  Would like the poster to either work further with the experts toward a working solution, or explain why none are useful.

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34368945
I guess without further input from the original poster, we likely should close the question as abandoned and not award points.  If points were to be awarded then Rob should likely get all or most, since he had the VBS solution, which was the zone posted in...

~bp
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 1400 total points
ID: 34369726
Okay, this feels funny I guess, but I did some testing, determined that neither solution worked properly, and will now post corrected versions.  At this point I think I would suggest closing, selecting this post as the accepted answer, with no points to Rob or I.  Rob, feel free to disagree.

In the BAT script the arguments to the FIND were not quite right above and found an extra line in the text files.  That is corrected below.

In addition neither approach dealt with the fact that the date in the text file that were going to add to the file name in the rename contained forward slash "/" characters, which throws an error in a filename.  I've adjusted both scripts to replace with dash "-" before the rename.

Naturally without the original poster replying we don't know if either (or both) approaches met their need.

Batch (BAT) approach:

@echo off
setlocal EnableDelayedExpansion
cd c:\Files
for %%A in (*.txt) do (
  for /F "tokens=*" %%B in ('findstr /I /C:"note date:" "%%A"') do (
    set NewName=%%~nA %%B.txt
    set NewName=!NewName:/=-!
    ren "%%A" "!NewName::=!"
  )
)

Open in new window

VB script (VBS) approach:

strFolder = "C:\Files"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each objFile In objFSO.GetFolder(strFolder).Files
        If Right(LCase(objFile.Name), 4) = ".txt" Then
                Set objText = objFSO.OpenTextFile(objFile.Path, intForReading, False)
                strAppend = ""
                While Not objText.AtEndOfStream
                        strLine = objText.ReadLine
                        If InStr(strLine, "NOTE DATE:") > 0 Then strAppend = strLine
                Wend
                objText.Close
                If strAppend <> "" Then
                        strNewName = Left(objFile.Name, Len(objFile.Name) - 4) & " " & Replace(Replace(strAppend, ":", ""), "/", "-") & ".txt"
                        If objFSO.FileExists(strNewName) = True Then objFSO.DeleteFile strNewName, True
                        objFSO.MoveFile objFile.Path, strNewName
                End If
        End If
Next
MsgBox "Done"

Open in new window

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34373217
Thanks Bill, great job.  I had forgotten about the forward slash, and admittedly didn't test it, so I'm fine with the way this has been closed.

Thanks all.

Rob.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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