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
Solved

Add return for specific character found

Posted on 2013-12-05
6
295 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 53

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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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 53

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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PerfMon Report Time Out 6 42
"Recent" shortcut button missing in Windows 10 7 51
Unable to Uninstall Visual Studio 2015 7 39
Windows 10 bootup error 22 48
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

790 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