Solved

vbscript - replace text in a file...

Posted on 2010-11-19
6
532 Views
Last Modified: 2012-05-10
I have a file that I need to reformat the html inside...right now it reads liek this:

<!-- BEGIN_PANEL_1 -->
<!-- BEGIN_PANEL_1 -->
<!-- BEGIN_PANEL_1 -->

....code...
<img src="images/
....

<!-- BEGIN_PANEL_2 -->
<!-- BEGIN_PANEL_2 -->
<!-- BEGIN_PANEL_2 -->

....code...
<img src="images/
....


<!-- BEGIN_PANEL_3 -->
<!-- BEGIN_PANEL_3 -->
<!-- BEGIN_PANEL_3 -->

....code...
<img src="images/..

....so on...


<!-- BEGIN_PANEL_1 -->
<!-- BEGIN_PANEL_1 -->
<!-- BEGIN_PANEL_1 -->

....code...
<img src="images_1/
....

<!-- BEGIN_PANEL_2 -->
<!-- BEGIN_PANEL_2 -->
<!-- BEGIN_PANEL_2 -->

....code...
<img src="images_2/
....


<!-- BEGIN_PANEL_3 -->
<!-- BEGIN_PANEL_3 -->
<!-- BEGIN_PANEL_3 -->

....code...
<img src="images_3/..


I need to read like this...

dim objArgs, objFile1, objFile2, FileStream1,FileStream2, i , slIN1, var_line_read, var_line_read_END, sLINE2, j, sline3, sline4, sline5, k, allLines, sline6

Set objArgs = Wscript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Set objFile1 = objFSO.GetFile(objArgs(0))
'Set objFile2 = objFSO.GetFile(objArgs(1))

Set FileStream1 = objFSO.OpenTextFile("C:\Documents and Settings\NBxxR\PRG_PJTS\WIT\LIVE\Home_Page_ALL_IN_ONE-test.txt", 1, true)
Set FileStream2 = objFSO.OpenTextFile("C:\Documents and Settings\NBxxR\PRG_PJTS\WIT\LIVE\test.txt", 8, true)


   
   
   
   
   sLine1 = FileStream1.ReadAll()
   FileStream1.Close
   
   '  reformat the file....
   
   
   ' -----------------------------------------------------------------------------
   
  For i = 1 to 200
  
  
   var_line_read = "<!-- BEGIN_PANEL_" & i & " -->"
      j = j + 1
   var_line_read_END = "<!-- BEGIN_PANEL_" & j & " -->"
   
   	if sLine1 = var_line_read  then
   		Do while sline1 <> var_line_read_END
   		   sline1 = Replace(sline1,"<img src=""~/Content/images_"" ", "<img src=""~/Content/images_"" " & i)
   		
   		loop
   
   	else
   	end if
 
  Next 
   
 
 ' -------------------------------------------------------------------------------
 
 
   For i = 1 to 200
   
   
    var_line_read = "<!-- BEGIN_PANEL_x_" & i & " -->"
       j = j + 1
    var_line_read_END = "<!-- BEGIN_PANEL_x_" & j & " -->"
    
    	if sLine1 = var_line_read  then
    		Do while sline1 <> var_line_read_END
    		    sline1 = Replace(sline1,"<img src=""~/Content/images_x_"" ", "<img src=""~/Content/images_x_"" " & i)
    		
    		loop
    
    	else
    	end if
  
  Next
 
 
  
  
   Filestream2.writeline sline1


msgbox("complete")

Open in new window

0
Comment
Question by:GlobaLevel
[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
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 34179582
this is a duplicate question of: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26627941.html

Please close one of these out, as you are violating the terms of this site.
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 34179583
Couple of issues I can see:
sLine1 is actually the entire file it's not an array, you would have to split it to create the array by using something like Split(sLine1, Chr(10)) or VBCrLf.
The other thing is that while the rest of your script references an array, you're writing the code at the end rather than within the loop, so rather than

sline1 = Replace(sline1,"<img src=""~/Content/images_"" ", "<img src=""~/Content/images_"" " & i)
You would write the line to the file
FileStream2.Write(sline1,"<img src=""~/Content/images_"" ", "<img src=""~/Content/images_"" " & i))

Hope that helps.

Cheers
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 500 total points
ID: 34179815
instead of doing readall, do a loop through each line.  (no need to make it an array)

something like
Do While Not(FileStream1.AtEndOfStream)
    sLine1 = Replace(FileStream1.ReadLine, "<img src=""~/Content/images_"" ", "<img src=""~/Content/images_"" " & i)
Loop
0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 10

Author Comment

by:GlobaLevel
ID: 34198797
It didnt quite work in the sense that it didnt replace the text, although no errors were found...
0
 
LVL 25

Expert Comment

by:kevp75
ID: 34199010
can you post your new code?
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 34261250
thanks
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

690 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