Solved

Add return for specific character found

Posted on 2013-12-05
6
290 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
  • 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 51

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
Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

 

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
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…
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). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

760 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

20 Experts available now in Live!

Get 1:1 Help Now