We help IT Professionals succeed at work.

need to find and replace numbers within a document and increment by 1

I have a text file which is a deposition transcript ( a simple TXT file ) laid out as follows.

00000
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl
00001
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl
00002
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl

The five digit numbers represent page numbers, the single digits represent line numbers. Each page actually has 25 lines per page and the page numbers actually go up to about 500.

My problem is , whoever created this file started with page 00000 and not page 00001 , so now the program that reads this file is incorrect by one page.

I want to know if anyone has any idea how I could easily fix the page numbering in this txt file.  I was thinking of some kind of advanced find and replace and increment by 1 ?

Any thoughts are appreciated
Comment
Watch Question

Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
SILVER EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Make a copy of the current file for backup

Open a temp file for Output
Open the current file file for Input
Read and ignore the first line
Write 00001 to the temp file using a counter that starts at 00001
Read and write the next 25 lines to the temp file
Read and ignore the next line
Increment the counter by one and write 00002 to the temp file
etcetrta until the current file is at EOF

close both files
Kill the current file
rename the temp file with the current file name

BRONZE EXPERT
Top Expert 2010

Commented:
Is this definitely a text file?

Also, must the solution be code, or are you open to a non-code solution?

Author

Commented:
yes it is a text file  
I would prefer a non code solution    I'm not very good with code

U
Test your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Okay, here's a VBS script approach that should do the job.  Save as a VBS and run like:

cscript EE27430540.vbs in.txt out.txt
cscript EE27430540.vbs in.txt

The firest example will read in.txt and write the updated version to out.txt, good for testing.  The second example will read and overwrite in.txt.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  strInFile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  strOutFile = WScript.Arguments(1)
Else
  strOutFile = strInFile
End If

' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
arrLines = Split(objFile.ReadAll, VbCrLf)
objFile.Close

' Loop through all lines of the file, adjust values on page lines
For i = 0 To UBound(arrLines)
    strOld = arrLines(i)
    If Left(strOld, 1) = "0" Then
        arrLines(i) = LeftPad(CStr(CInt(strOld) + 1), Len(strOld), "0")
    End If
Next

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write(Join(arrLines, vbCrLf))
objFile.Close

Function LeftPad( strText, intLen, chrPad )
  'LeftPad( "1234", 7, "x" ) = "xxx1234"
  LeftPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
GrahamSkanRetired
SILVER EXPERT
Top Expert 2012

Commented:
You will have to use VBA code unless you want to do multiple Find and Replace operations
Sub MultiReplace()
    Dim i As Integer
    
    For i = 1 To ActiveDocument.Paragraphs.Count Step 8
        ActiveDocument.Paragraphs(i).Range.Text = Format((i / 8) + 1, "00000") & vbCr
    Next i
End Sub

Open in new window


There is some get-you-started advice for  VBA at the end of this article:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/A_7236-Macro-code-to-convert-text-to-fields-in-Microsoft-Word.htm

Author

Commented:
Code worked perfectly. Thank you
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, glad that helped, thanks for the feedback.

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.