Solved

Script to have only 80 characters per line

Posted on 2011-02-25
9
646 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
[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
  • 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
Industry Leaders: 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: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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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