Solved

Script to have only 80 characters per line

Posted on 2011-02-25
9
605 Views
Last Modified: 2012-06-22
Need help with a script to scan files in a directory and
reformat the document so that there are only 80 characters
per line, starting at line 6.

Any help getting started would be appreciated.

Thank you
0
Comment
Question by:cbones
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 34981576
are the files text files?
0
 

Author Comment

by:cbones
ID: 34982093
yes they are.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34982291
And what should happen if a line has, say, 187 characters?
0
 

Author Comment

by:cbones
ID: 34982307
I would like it to create a new line underneath it with 80 characters.  So the 187 characters would become three 3 lines. (80, 80, and 27).
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:prashanthd
ID: 34982334
Try this code, it will scan files in directory and reformat the document so that there are only 80 characters per line, starting at line 6.

'Path of folder which has files to be modified
strDirectory="c:\test\"

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder, objFile
Const ForReading = 1
Const ForWriting = 2

Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile In objFolder.Files
    WScript.Echo objFile.Path
    Set objTextFile = objFSO.OpenTextFile(objFile.Path, ForReading)
    i=1
    strNewLine=""
    Do Until objTextFile.AtEndOfStream
        strNextLine = objTextFile.Readline
            If i<=5 Then
                  strNewLine=strnewline & strnextline
            Else
                  strNewLine=strnewline & Mid(strnextline,1,80)            
            End If
            i=i+1
    Loop
    objTextFile.Close
      Set objTextFile = objFSO.OpenTextFile(objFile.Path, ForWriting)
    objTextFile.Write strNewLine
      objTextFile.Close
Next      
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 34982396
Try the following code,made some changes

'Path of folder
strDirectory="c:\test\"

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder, objFile
Const ForReading = 1
Const ForWriting = 2

Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile In objFolder.Files
    WScript.Echo objFile.Path
    Set objTextFile = objFSO.OpenTextFile(objFile.Path, ForReading)
    i=1
    strNewLine=""
    Do Until objTextFile.AtEndOfStream
        strNextLine = objTextFile.Readline
            If i<=5 Then
                  strNewLine=strnewline & strnextline
            Else
                  strNewLine=strnewline & Replace(Mid(strnextline,1,80),vbCrLf,"") & vbCrLf            
            End If
            i=i+1
    Loop
    objTextFile.Close
      Set objTextFile = objFSO.OpenTextFile(objFile.Path, ForWriting)
    objTextFile.Write strNewLine
      objTextFile.Close
Next      
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 34982540
Try this.  It works against all text files in the specified directory.



Dim fso, fld, sf, fil, tsIn, tsOut, Lines, LineLen, TimeStamp, TheLine, Counter

Const SourceFolder = "c:\Source" 'change as needed
Const MaxWidth = 80

TimeStamp = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & _
    Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(SourceFolder)
Set sf = fso.CreateFolder(SourceFolder & "\Results " & TimeStamp)

For Each fil In fld.Files
    If LCase(fil.Name, 4) = ".txt" Then
        Set tsIn = fso.OpenTextFile(fil.Path)
        Set tsOut = fso.CreateTextFile(sf.Path & "\" & fil.Name)
        Lines = 0
        Do Until tsIn.AtEndOfStream
            Lines = Lines + 1
            TheLine = tsIn.ReadLine
            LineLen = Len(TheLine)
            If Lines < 6 Then
                tsOut.WriteLine TheLine
            Else
                For Counter = 1 To LineLen Step MaxWidth
                    tsOut.WriteLine Mid(TheLine, Counter, MaxWidth)
                Next
            End If
        Loop
        tsIn.Close
        tsOut.Close
    End If
Next

Set tsIn = Nothing
Set tsOut = Nothing
Set sf = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

MsgBox "Done"

Open in new window

0
 

Author Comment

by:cbones
ID: 34998754
If it was to get changed to a word document (.doc extension) how would that change the script?

Thank you.
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 400 total points
ID: 34998970
It can  be done without any major changes, we need to reference in following way

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\myfolder\mydoc.doc")
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

21 Experts available now in Live!

Get 1:1 Help Now