Solved

Add return for specific character found

Posted on 2013-12-05
6
294 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
domain controllers numbers 4 76
Monitor Resolution 5 26
Batch script to check for file size and delete/keep 3 21
Do hyper-v and VMware clash 4 59
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

810 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