Solved

Script to have only 80 characters per line

Posted on 2011-02-25
9
627 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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