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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to Read & Write to same file - can't find in documentation 4

Status
Not open for further replies.

tradle

Programmer
Jan 7, 2004
94
US
I've been given a task to write a script using VBScript, a language that I know not a darn thing about, to take 3 different strings or string collections and insert them into various places within all files in a given directory.

I've downloaded the Windows Script Documentation after reading several posts in this forum, but I'm afraid that I haven't yet found what I need to complete this task.

Essentially, this is what I need to do - open each file as a text stream, find a string within the file, insert a new string on a new line, and then do two more "search and insert" methods to repeat that behavior, and then save the file and move to the next one in the directory.

I've found how to either read or write to a file but not both within the same file. If there's some part of the Windows Script Documentation that you could point me to, that would be hugely helpful, especially if it contains some examples for me to model my code after.

Thanks!!!!!
 
Thank you so much mrmovie and tsuji!

Is there a way to tell the code to only execute once? I have multiple instances of % %%EndPageSetup, and only want to run the insert code once.
 
you should set a boolean value then



for each file in fc

bPageSetup = False
Set tsIni = FSO.OpenTextFile(file)
file2create = "c:\temp\" & file.Name & "temp"
Set newInifile = fso.CreateTextFile(file2Create, True)

Do While Not tsIni.AtEndOfStream
sLine = tsIni.ReadLine
newIniFile.WriteLine sLine
Select Case sLine
Case "%%LanguageLevel: 2"
newInifile.WriteLine "%ADOContainsXMP:MainFirst" & VBNewLine
Case "% %%EndPageSetup"
If bPageSetup = False Then
newInifile.WriteLine "blaa"
newInifile.WriteLine "blaa blaa"
bPageSetup = True
End If
Case "(%%[Page: 1]%%) ="
newInifile.WriteLine "the rest of the other file?"
End Select

Loop

newInifile.Close
tsIni.Close
 
MRMOVIE,

I'm getting an "expected statement" error at line 31 - can you see what's wrong?

Additionally, have I placed the "replace" loop in the correct location?

I have to reference this statement out to a different file.

Code:
Function TextStreamTest()
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   Dim fso, ts, source, fc
   Set fso = CreateObject("Scripting.FileSystemObject")
   set source = fso.GetFolder("f:\advent\wrc\ps\12312003\test")
   set fc = source.Files

	for each file in fc
		bPageSetup = False
		Set tsIni = FSO.OpenTextFile(file)
		file2create = "f:\temp\" & file.Name & "temp"
		Set newInifile = fso.CreateTextFile(file2Create, True)

		Do While Not tsIni.AtEndOfStream
		    sLine = tsIni.ReadLine
		    newIniFile.WriteLine sLine
		    Select Case sLine
		       	Case "%%LanguageLevel: 2"
			 newInifile.WriteLine "%%ADOContainsXMP:MainFirst"
       			Case "% %%EndPageSetup"
		         If bPageSetup = False Then	
		         newInifile.WriteLine "%%ControlNumber" &VBNewLine
		         newInifile.WriteLine "gsave"
		         newInifile.WriteLine "Times-Roman 50 selectfont"
		         newInifile.WriteLine "180 rotate"
		         newInifile.WriteLine "-1 1 scale"
		         newInifile.WriteLine "907 -1021 moveto"
		         newInifile.WriteLine "(000000.0000.00.0000.000.000000000.000000) show"
		         newInifile.WriteLine "grestore" & VBNewLine
		       Case "(%%[Page: 1]%%) = "
			 If bPageSetup = False Then
			 newInifile.WriteLine "" & VBNewLine		
		         newInifile.WriteLine "/ncbdict 1 dict def" & VBNewLine		
	  		 newInifile.WriteLine "ncbdict begin" & VBNewLine
	  		 newInifile.WriteLine "/currentdistillerparams where"
	  		 newInifile.WriteLine "{pop currentdistillerparams /CoreDistVersion get 5000 lt} {true} ifelse" & VBNewLine
	  		 newInifile.WriteLine "{ncbdict /pdfmark /cleartomark load put"
	  		 newInifile.WriteLine "	ncbdict /metafile_pdfmark {flushfile cleartomark} bind put}"
	  		 newInifile.WriteLine "{ncbdict /metafile_pdfmark {/PUT pdfmark} bind put} ifelse" & VBNewLine		
	  		 newInifile.WriteLine "%Define the stream object and associate it as metadata"
	  		 newInifile.WriteLine "[/_objdef {ncb_metadata_stream1} /type /stream /OBJ pdfmark"
	  		 newInifile.WriteLine "[{ncb_metadata_stream1} 2 dict begin"
	  		 newInifile.WriteLine "	/Type /Metadata def /Subtype /XML def currentdict end /PUT pdfmark" & VBNewLine				
	  		 newInifile.WriteLine "%Fill the stream with the XMP Packet..."
	  		 newInifile.WriteLine "[{ncb_metadata_stream1}"
 	  		 newInifile.WriteLine "	currentfile 0 (% &&end XMP packet marker&&)"
	  		 newInifile.WriteLine "	/SubFileDecode filter metafile_pdfmark"
 	 		 newInifile.WriteLine "<?xpacket begin='?' id='W5M0MpCehiHzreSzNTczkc9d'?>"
 			 newInifile.WriteLine "<x:xmpmeta xmlns:x='adobe:ns:meta/'>"
	  		 newInifile.WriteLine "<rdf:RDF xmlns:iX='[URL unfurl="true"]http://ns.adobe.com/iX/1.0/'>"[/URL]
	  		 newInifile.WriteLine "<rdf:Description rdf:about='' xmlns:ncb='//ohclesql1026/ncb/metadata/' ncb:CompTime='COMP_TIME|' ncb:PkgSeq='PKG_SEQUENCE|$1' ncb:StmtSeq='STMT_SEQUENCE|$1' ncb:Mail1='MAIL_TO1|$2' ncb:Mail2='MAIL_TO2|$3' ncb:Mail3='MAIL_TO3|$4' ncb:Mail4='MAIL_TO4|$5' ncb:Mail5='MAIL_TO5|$6' ncb:Mail6='MAIL_TO6|$7' ncb:ZipCode='ZIP_CODE|' ncb:InsMap='INSERT_MAP|YNNNNNNN' ncb:InsId='INSERT_ID|NCIM1Q04                                                                         ' ncb:AcctId='ACCOUNT_ID|$8' ncb:PaperCde='PAPER_CODE|10001453' ncb:EnvCde='ENV_CODE|10001538' ncb:SpecHand='SPECIAL_HANDLING|N'/>"
	  		 newInifile.WriteLine "</rdf:RDF>"
	  		 newInifile.WriteLine "</x:xmpmeta>"
	  		 newInifile.WriteLine "<?xpacket end='w'?>" & VBNewLine				
	  		 newInifile.WriteLine "% &&end XMP packet marker&&" & VBNewLine				
	  		 newInifile.WriteLine "%Attach the stream as the main XMP metadata stream"
	  		 newInifile.WriteLine "[{Catalog} {ncb_metadata_stream1} /Metadata pdfmark" & VBNewLine
		    End Select        

		Loop
			Do While Not tsIni.AtEndOfStream
			    sLine = tsIni.ReadLine
			    newIniFile.WriteLine Replace(sLine, "$1", "99999999")
	   		Loop
    
		newInifile.Close
		tsIni.Close
		
		Next

	
End Function

WScript.echo TextStreamTest

Thanks for all of your help!!!
 
SORRY - Found the issue of a missing End If.

I'm still seeing the duplication for the %%controlnumber - did I define that correctly?
 
I again found the error - a missing def for bPageSetup.

mrmovie, if you could comment on my placement of the replace code, I'd really appreciate it. I'd really like some guidance for how I would introduce a new read statement. I'll play and see if I can resolve on my own...

THANK YOU SO MUCH!!!
 
Sorry for all of the posts!!!

After thinking more about this, I think that using a recordest to change the values is much more logical. I'm looking through the threads at the same time as writing this one - here's what I want to do:

Create a connection to one table of one of our ODBC databases through its DSN name, query each of the eight values for each file, and replace the variable values that I've introduced in one of the strings with entries from the table.

I have been so fortunate to have worked with such patient people - thanks to all who have contributed.
 
Not just worthy of a star but a comment to. As a VB Script novice who scans the site for tips and tricks on a regular basis this has been a brilliant thread to read and learn from! Good work as ever Mr Movie And Tsuji and glad Tradle got things sorted in the end
 
I DON'T DISAGREE ONE BIT!!!!!!!!!!

I don't know how stars are assigned, but I'd give each gentleman several were it up to me.

Many thanks to mrmovie, tsuji and PHV - I couldn't have done this without their help and support. For any out there who are curious about the task here or the coding, I altered postscript documents with the vbscript in all of the above posts. I hope that someone benefits from the collective efforts of all who posted suggestions.

Tim
(Tradle)
 
I don't know how stars are assigned, but I'd give each gentleman several were it up to me.
tradle, simply click on the "Thank mrmovie for this valuable post!" link and on "Thank tsuji for this valuable post!"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hello all,

Thanks everybody, the stars and all.

I now have a chance to look back what I hastily posted. I know it is fast outdated as tradle progresses so very quick and solid. I just cannot stand those typos and less than perfect logic. Setting bad example is not good in the public forum. I revise the listing incorporating mrmovie rightly pointed out using replace() better alternative. (Multiple pass is not efficient, I know. I just leave it there.)
Code:
sFolder="f:\advent\wrc\ps\12312003\test"
Transform_signature sFolder

Sub Transform_signature (sFolderPath)
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
	Dim fso, f, fc, ts
	Dim info, info2, sig, sig_len, sig2, sig2_len, sig3, sig3_len, pos

	info="%ADOContainsXMP:MainFirst"
	info2="%%ControlNumber" & vbcrlf & _
		"gsave" & vbcrlf & "Times-Roman 50 selectfont" & vbcrlf & _
		"180 rotate" & vbcrlf & "-1 1 scale" & vbcrlf & _
		"907 -1021 moveto" & vbcrlf & _
		"(000000.0000.00.0000.000.000000000.000000) show" & vbcrlf & _
		"grestore"

	sig="%%LanguageLevel: 2"
	sig_len=len(sig)
	sig2="%%%EndPageSetup"
	sig2_len=len(sig2)
	'sig3="[Page: 1]%% ="
	'sig3_len=len(sig3)

	set fso = CreateObject("Scripting.FileSystemObject")

	on error resume next
	set source = fso.GetFolder(sFolderPath)
	if err<>0 then
		wscript.echo "Folder does not exist. Operation aborted."
		set fso=nothing
		exit sub
	end if
	on error goto 0

	set fc = source.Files
	for each f in fc
	'first pass <<<<<<<sig
	set ts=f.OpenAsTextStream(ForReading, TristateUseDefault)
	f_contents=ts.readall
	ts.close
	f_len=len(f_contents)
	if instr(1,f_contents,sig,1)<>0 then
		pos=instr(1,f_contents,sig,1)
		f_contents=left(f_contents,pos-1) & sig & vbcrlf & info & vbcrlf & _
			right(f_contents,f_len-pos-sig_len+1)

		'alternative & better; applicable even  for multiple instances
		'f_contents=replace(f_contents,sig,sig&vbcrlf&info&vbcrlf)

		set ts=f.OpenAsTextStream(ForWriting, TristateUseDefault)
		ts.write f_contents
		ts.close
	else
		wscript.echo "Cannot location " & sig & " in " & f.path
	end if
        'second pass <<<<<<<<sig2
	set ts=f.OpenAsTextStream(ForReading, TristateUseDefault)
	f_contents=ts.readall
	ts.close
	f_len=len(f_contents)
	if instr(1,f_contents,sig2,1)<>0 then
		pos=instr(1,f_contents,sig2,1)
		f_contents=left(f_contents,pos-1) & sig2 & vbcrlf & info2 & vbcrlf & _
			right(f_contents,f_len-pos-sig2_len+1)

		'alternative & better; applicable even  for multiple instances
		'f_contents=replace(f_contents,sig2,sig2&vbcrlf&info2&vbcrlf)

		set ts=f.OpenAsTextStream(ForWriting, TristateUseDefault)
		ts.write f_contents
		ts.close
	else
		wscript.echo "Cannot location " & sig2 & " in " & f.path
	end if
	next
	set fc=nothing
	set fso=nothing
End Sub
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top