Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to have only 80 characters per line

Posted on 2011-02-25
9
Medium Priority
?
651 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 93

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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 400 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 1600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

597 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