Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript insert blank line and hyphen in text file 1

Status
Not open for further replies.

crapplet

MIS
Aug 13, 2010
5
US
Is there a way to insert a vbCrLf and a hyphen in a text file using VBScript?

The text file is formatted as the following:

SampleData: Data
SampleData2: Data2
MoreSampelData::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd=
MoreSampelData2::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd==
MoreSampelData2::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd==

I need the data to look like the following:

SampleData: Data
SampleData2: Data2
MoreSampelData::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd=
-

MoreSampelData2::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd==
-

MoreSampelData2::
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
qwerrttyuiopassddffgjdjdjdjshjfjkhdhfjd
dddd==
-

I need the hyphen and blank line after the data that is formatted with the space before the characters.

Is this possible? Thank you in advance for your time.
 
Does the last line of the actual sample data also end with an equals sign? (or does the Sample Data header line always end with a colon?) If so, we can use this to signal where to add the hyphen and new line. If not, is there anything there we can use?
 
[tt]dim infile, outfile, s, fso, rx
'your givens[green]
infile="abc.xyz"
outfile="abc_out.xyz"[/green]

set fso=createobject("scripting.filesystemobject")
if not fso.fileexists(infile) then
wscript.echo "file " & infile & " not found."
set fso=nothing
wscript.quit
end if
s=""
if fso.getfile(infile).size<>0 then
s=fso.opentextfile(infile,1,false).readall
set rx=new regexp
with rx
.pattern="(dddd={1,2})"
.global=true
.ignorecase=false
end with
s=rx.replace(s, "$1" & vbcrlf & "-" & vbcrlf)
fso.createtextfile(outfile,true).write s
else
'empty input file : do nothing
end if
set fso=nothing
[/tt]
 
You can always tighten up the condition of replacing. (For a throw-away script, you do what it get the job done, that all.) Such as this: you can profit the support of positive look-ahead to make the condition after the equal sign be a line-break (window-style hereinbelow, for illustration) not anything else.
[tt]
[red]'[/red].pattern="(dddd={1,2}"
.pattern="(dddd={1,2}(?=\r\n))" 'windows style newline
[/tt]
The exact generalization depends on the real needs, sure.
 
Thanks for the responses, the example data is just an example. The data is actually an ldf file and I am trying to prepare the file for import, so the data is constantly changing.
 
>so the data is constantly changing
Sure, that's what I learn as well from Greek philosophers.

>The data is actually an ldf file and I am trying to prepare the file for import
The devices of reading and saving to a file are peripheral. The central piece of functionality is accomplished by the regexp engine. When you "prepare the file for import", if it is loaded in the memory, pass the string in the memory (s) through it:
[tt] s=rx.replace(s, "$1" & vbcrlf & "-" & vbcrlf)[/tt]
and that's where you would get the job done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top