?
Solved

Add return for specific character found

Posted on 2013-12-05
6
Medium Priority
?
299 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 56

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 1000 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 56

Accepted Solution

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

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

to:

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

~bp
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

771 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