[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

copy variable string to new location in text file

I want to do the following:
1. copy a variable string in a text file
2. append the string with the following characters at the end  <BR>
3. insert the appended string into a new location in the same text file
4. save the changes to the file

 The variable string is a date timestamp. For example:
Date: Tue, 24 Mar 2009 18:48:49

The appended string should look like this:
Date: Tue, 24 Mar 2009 18:48:49<BR>

The insertion point of the copied text is after the term, "quoted-printable" and on a new line. For example:
Content-Transfer-Encoding: quoted-printable
Date: Tue, 24 Mar 2009 18:48:49<BR>

The variable string can be easily identified in each text file because it always starts with the word, "Date:" and is always followed by the word, "-0700"

There are multiple text files in a folder and each file would need this change.
0
AManoux
Asked:
AManoux
  • 5
  • 4
1 Solution
 
merowingerCommented:
the problem i see is that you can't delete or change specific lines in a textfile with vbscript.
What i did in the past was to read in all the text (or the line for line), changed the values in vbscript variables, deleted the original file with vbscript and created a new one with the changed content...
0
 
AManouxAuthor Commented:
That's fine.
Possible helpful info:  The date variable string is always on the 4th line of the text file and always on a line by itself.

0
 
AManouxAuthor Commented:
Ok, I have pieced together bits of code to accomplish what I need.  The only thing is that my current script only works by specifying and individual file path and file name.  I need the script to loop through all the text files in a specific folder.  Can someone tell me how to tweak the script to accomplish this?
Dim aString
Const ForReading = 1, Forwriting = 2
 
aString= "<BR>"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\files\test.txt", ForReading)
 
For i = 1 to 3
    objTextFile.ReadLine
Next
 
strLine = objTextFile.ReadLine
strLine2 = strLine & aString
 
objTextFile.Close
 
Dim strFileSourcePath, strFileTargetPath, objFSOSource, objFSOTarget, fso, objFilesSource, objFilesTarget, strCurrentLine
 
strFileSourcePath = "C:\files\test.txt"
strFileTargetPath = "C:\files\test.new"
 
Set objFSOSource = CreateObject("scripting.filesystemobject")
Set objFSOTarget = CreateObject("scripting.filesystemobject")
 
Set objFilesSource = objFSOSource.OpenTextFile(strFileSourcePath,ForReading,True,0) 
Set objFilesTarget = objFSOSource.OpenTextFile(strFileTargetPath,ForWriting,True,0) 
Set fso = CreateObject("Scripting.FileSystemObject")
 
Do While objFilesSource.AtEndOfStream <> True
  strCurrentLine = objFilesSource.ReadLine
 
  if StrComp(Left(strCurrentLine,43),"Content-Transfer-Encoding: quoted-printable") = 0 then
    objFilesTarget.WriteLine strCurrentLine
    objFilesTarget.WriteLine strLine2
  else
    objFilesTarget.WriteLine strCurrentLine
  end if
 
Loop
 
objFilesSource.Close
objFilesTarget.Close
 
Set objFSOSource = Nothing
Set objFSOTarget = Nothing
Set fso = Nothing

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
merowingerCommented:
It now loops trought all file in this folder.
I'm not 100% sure if the line replacement is working properly...have a look

strFolderName = "C:\Files"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderName)
 
'loop trought each file
For Each file In objFolder.Files
	'get next file in folder
	Set objSourceFile = objFSO.OpenTextFile(file)
	
	strCounter = 0
	
	'loop trought each line
	Do While Not objSourceFile.AtEndOfStream
		
		strCounter = strCounter + 1
		
		'read each line
		strCurrentLine = objSourceFile.ReadLine
		
		'is it the template line?
		If strCounter = 4 Then
			strNewLine = strCurrentLine &"<BR>"
		End If
		
		'compare each lines
		If StrComp(Left(strCurrentLine,43),"Content-Transfer-Encoding: quoted-printable") = 0 Then
    		strNewText = strNewText + strCurrentLine + vbNewLine
    		strNewText = strNewText + strNewLine + vbNewLine
  		Else
    		strNewText = strNewText + strCurrentLine + vbNewLine
  		End If
		
	Loop
	
	'get the current filename
	strFilename = objFSO.GetFile(file).Name
 
	'delete the current file
	objSourceFile.Close
	Set objSourceFile = Nothing
	objFSO.DeleteFile(file)
	
	'create a new textfile
	Set objTargetFile = objFSO.CreateTextFile(strFolderName &"\" &strFilename)
	objTargetFile.Write strNewText
Next

Open in new window

0
 
AManouxAuthor Commented:
It's very close to working correctly but I think these lines still have an issue.


'compare each lines
		If StrComp(Left(strCurrentLine,43),"Content-Transfer-Encoding: quoted-printable") = 0 Then
    		strNewText = strNewText + strCurrentLine + vbNewLine
    		strNewText = strNewText + strNewLine + vbNewLine
  		Else
    		strNewText = strNewText + strCurrentLine + vbNewLine
  		End If

Open in new window

0
 
merowingerCommented:
I get this result:

Content SourceFile
*******************************************
Line1
Line2
Line3
ThisIsTheLine
Line5
Line6
Line7
Line8
Line9
Content-Transfer-Encoding: quoted-printable
Line11
Line12
Line13
Content-Transfer-Encoding: quoted-printable
Line14
Line15
*******************************************

Content Targetfile
*******************************************
Line1
Line2
Line3
ThisIsTheLine
Line5
Line6
Line7
Line8
Line9
Content-Transfer-Encoding: quoted-printable
ThisIsTheLine<BR>
Line11
Line12
Line13
Content-Transfer-Encoding: quoted-printable
ThisIsTheLine<BR>
Line14
Line15
*******************************************
0
 
AManouxAuthor Commented:
I have 5 test files in a directory.
Your script appears to work fine for the File1.txt
File2.txt updates correctly except that all the text from the updated File1.txt gets appended to the beginning of the file.
File3.txt updates correctly except that all the text from File1.txt and File2.txt get appended to the beginning of the file.
...etc, etc....
0
 
merowingerCommented:
Oh yes that's correct.
Now i clean up the variable at the end of each file replacement
strFolderName = "C:\Files"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolderName)
 
'loop trought each file
For Each file In objFolder.Files
	'get next file in folder
	Set objSourceFile = objFSO.OpenTextFile(file)
	
	strCounter = 0
	
	'loop trought each line
	Do While Not objSourceFile.AtEndOfStream
		
		strCounter = strCounter + 1
		
		'read each line
		strCurrentLine = objSourceFile.ReadLine
		
		'is it the template line?
		If strCounter = 4 Then
			strNewLine = strCurrentLine &"<BR>"
		End If
		
		'compare each lines
		If StrComp(Left(strCurrentLine,43),"Content-Transfer-Encoding: quoted-printable") = 0 Then
    		strNewText = strNewText + strCurrentLine + vbNewLine
    		strNewText = strNewText + strNewLine + vbNewLine
  		Else
    		strNewText = strNewText + strCurrentLine + vbNewLine
  		End If
		
	Loop
	
	'get the current filename
	strFilename = objFSO.GetFile(file).Name
 
	'delete the current file
	objSourceFile.Close
	Set objSourceFile = Nothing
	objFSO.DeleteFile(file)
	
	'create a new textfile
	Set objTargetFile = objFSO.CreateTextFile(strFolderName &"\" &strFilename)
	objTargetFile.Write strNewText
	strNewText = ""
Next 

Open in new window

0
 
AManouxAuthor Commented:
Great Work. Thanks a million!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now