Solved

Script to have only 80 characters per line

Posted on 2011-02-25
9
616 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running Visio Macro from VBS File 3 54
how to add fields in script 20 51
Index Match Vlook up for a master file 7 58
OID and Active Directory Attributes 9 56
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

948 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