Solved

Add return for specific character found

Posted on 2013-12-05
6
297 Views
Last Modified: 2013-12-11
I want to search through a text file and when if finds the character ~ I want it to Return and go to the next line.

Every time it finds ~ it should act as if I hit return on the keyboard.  
Then i want the file saved for instance to c:\folder\copy.txt
0
Comment
Question by:100questions
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

by:JimFive
ID: 39699644
Something like this:
srcFileName = "C:\temp\inputfile.txt"
destFileName = "C:\temp\outfile.txt"

set fso = CreateObject("Scripting.FileSystemObject")
Set srcFile = fso.opentextfile(srcFileName)
Set destFile = fso.createtextfile(destFileName)

ln = srcFile.ReadLine
Do
    data = split(ln, "~")
    for i = lbound(data) to ubound(data)
        destfile.writeline(data(i) & "~")
    next
    ln = srcFile.Readline
loop until srcFile.atendofstream

Open in new window

0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39699942
Here's a similar but slightly different approach. I assumes you wanted to remove the ~ and replace with a new line, but if you want the tilde we can add that back easily in the REPLACE() function.

You pass the file name to process on the command line, and if only one file name is provided it will overwrite that file.  If two file names are provided, then the original file is only read, and the changed version is written to the second file name.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No filename specified."
   WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Read file into a string
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Perform replaces as needed
strData = Replace(strData, "~", vbCrLf)

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write strData
objFile.Close

Open in new window

~bp
0
 

Author Comment

by:100questions
ID: 39709913
@JimFive - your solution works if I run cscript.exe however it produces an error as well 'Input past end of file'.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:100questions
ID: 39709923
@billprew - your solution works Bill however it would be nice to keep the tilde in there,... can you revise the script to do that?
Thanks.
0
 
LVL 15

Assisted Solution

by:JimFive
JimFive earned 250 total points
ID: 39709940
@100questions:
OK, that's happening because I didn't consider that your input file is only one line to fix that change lines 8 and 9
ln = srcFile.ReadLine
Do

Open in new window

To
 While not srcFile.atEndOfStream
        ln = srcFile.ReadLine

Open in new window


And Change Lines 14 and 15:
    ln = srcFile.Readline
     loop until srcFile.atendofstream

Open in new window

To
Wend

Open in new window

0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 39710089
Sure, just change this line:

strData = Replace(strData, "~", vbCrLf)

to:

strData = Replace(strData, "~", "~" & vbCrLf)

~bp
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

740 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