• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

VBS Script

I have 5 text files that are dropped on the server.  4 of the files are one liners when a database loades this is an update at this time and date file.  the other is a combination of the 4.  This way when a person opens the one file they can see when each system was update.  What I need is the code to take file 1 and insert as line 1 in file 5, file 2 in line 2 etc.  I have a scheduler that can look for the files when they show do something but I need the script, even if it is one for each file.  That way when file 1 shows it runs script 1 and so on.

Thanks all
0
craigs052998
Asked:
craigs052998
  • 7
  • 5
1 Solution
 
mvidasCommented:
Hi craigs,

Give the following a try, should be what you need:

Dim FSO, File1, File2, File3, File4, File5
File1 = "C:\file1.txt"
File2 = "C:\file2.txt"
File3 = "C:\file3.txt"
File4 = "C:\file4.txt"
File5 = "C:\file5.txt"
If FSO.FileExists(File1) Then AddTextFileToOtherTextFile File1, File5
If FSO.FileExists(File2) Then AddTextFileToOtherTextFile File2, File5
If FSO.FileExists(File3) Then AddTextFileToOtherTextFile File3, File5
If FSO.FileExists(File4) Then AddTextFileToOtherTextFile File4, File5
Set FSO = Nothing
Set File1 = Nothing
Set File2 = Nothing
Set File3 = Nothing
Set File4 = Nothing
Set File5 = Nothing

Function AddTextFileToOtherTextFile(ByVal strSourceFile, ByVal strDestinationFile)
 Dim TS, tempStr
 Set FSO = CreateObject("scripting.filesystemobject")
 Set TS = FSO.OpenTextFile(strSourceFile, 1) '1=ForReading
 tempStr = TS.ReadAll
 TS.Close
 Set TS = FSO.OpenTextFile(strDestinationFile, 8, True) '8=ForAppending, True=Create if doesnt exist
 TS.WriteLine tempStr
 TS.Close
 'FSO.DeleteFile strSourceFile
 Set TS = Nothing
 Set tempStr = Nothing
End Function


Matt
0
 
mvidasCommented:
My apologies, the "Set FSO = CreateObject..." line should be after the "File5 = ..." line, not within the function.  Very sorry about that.
0
 
craigs052998Author Commented:
Will this append?  I only want 4 line to ever appear in the main file?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mvidasCommented:
Ok, I think I may have misunderstood you when you said:
>>That way when file 1 shows it runs script 1 and so on

If you want it to write to the main when the other 4 exist, use the following instead:

Dim File1, File2, File3, File4, File5, tempStr, FSO, TS
Set FSO = CreateObject("scripting.filesystemobject")
File1 = "C:\file1.txt"
File2 = "C:\file2.txt"
File3 = "C:\file3.txt"
File4 = "C:\file4.txt"
File5 = "C:\file5.txt"
If FSO.FileExists(File1) And FSO.FileExists(File2) And FSO.FileExists(File3) And FSO.FileExists(File4) Then
 tempStr = Join(Array(GetTextFile(File1), GetTextFile(File2), GetTextFile(File3), GetTextFile(File4)), vbCrLf)
 Set TS = FSO.OpenTextFile(File5, 2, True) '2=ForWriting
 TS.Write tempStr
 TS.Close
End If
Set TS = Nothing
Set tempStr = Nothing
Set FSO = Nothing
Set File1 = Nothing
Set File2 = Nothing
Set File3 = Nothing
Set File4 = Nothing
Set File5 = Nothing

Function GetTextFile(ByVal strSourceFile)
 Set TS = FSO.OpenTextFile(strSourceFile, 1) '1=ForReading
 GetTextFile = TS.ReadAll
 TS.Close
End Function

Matt
0
 
craigs052998Author Commented:
What can happen is file 5 will always be there.  files 1 - 4 wiil, might come at different times.  But the data in file 1 will ALWAYS be line 1 in file 5, file 2 data will be line 2 in file 5 and so on.  And file 5 will ONLY have 4 lines of text in it.

Does this help or explain it further?
0
 
mvidasCommented:
I do understand.. I just want to make sure this is what you want.

Lets say file5 currently has the following contents:
oldtext1
oldtext2
oldtext3
oldtext4

Then file3 appears, so you want file5 to now contain:
oldtext1
oldtext2
newtext3
oldtext4

Also, do you want file3 deleted at that point?
0
 
craigs052998Author Commented:
BINGO, right on the head
0
 
mvidasCommented:
Alright, sorry about that! This should do it for you:

Dim FSO, File1, File2, File3, File4, File5
File1 = "C:\file1.txt"
File2 = "C:\file2.txt"
File3 = "C:\file3.txt"
File4 = "C:\file4.txt"
File5 = "C:\file5.txt"
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FileExists(File1) Then AddTextFileToOtherTextFile File1, File5, 1
If FSO.FileExists(File2) Then AddTextFileToOtherTextFile File2, File5, 2
If FSO.FileExists(File3) Then AddTextFileToOtherTextFile File3, File5, 3
If FSO.FileExists(File4) Then AddTextFileToOtherTextFile File4, File5, 4
Set FSO = Nothing
Set File1 = Nothing
Set File2 = Nothing
Set File3 = Nothing
Set File4 = Nothing
Set File5 = Nothing

Function AddTextFileToOtherTextFile(ByVal strSourceFile, ByVal strDestinationFile, ByVal LineNum)
 Dim TS, tempStr, tempStr2(), i
 ReDim tempStr2(3)
 Set TS = FSO.OpenTextFile(strSourceFile, 1)
 tempStr = TS.ReadLine
 TS.Close
 Set TS = FSO.OpenTextFile(strDestinationFile, 1)
 For i = 0 To 3
  tempStr2(i) = TS.ReadLine
 Next
 TS.Close
 tempStr2(LineNum - 1) = tempStr
 Set TS = FSO.OpenTextFile(strDestinationFile, 2)
 TS.Write Join(tempStr2, vbCrLf)
 TS.Close
 FSO.DeleteFile strSourceFile
 Set TS = Nothing
 Set tempStr = Nothing
 Set i = Nothing
End Function

Matt
0
 
craigs052998Author Commented:
Comment in code.  Look for (  I GET ERROR HERE )

Dim FSO, File1, File2, File3, File4, File5
File1 = "C:\sritest\servicing.txt"
File2 = "C:\sritest\EDW.txt"
File3 = "C:\sritest\DMAccounting.txt"
File4 = "C:\sritest\Originations.txt"
File5 = "C:\sritest\lastrefresh.txt"

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FileExists(File1) Then AddTextFileToOtherTextFile File1, File5, 1

If FSO.FileExists(File2) Then AddTextFileToOtherTextFile File2, File5, 2

If FSO.FileExists(File3) Then AddTextFileToOtherTextFile File3, File5, 3

If FSO.FileExists(File4) Then AddTextFileToOtherTextFile File4, File5, 4

Set FSO = Nothing
Set File1 = Nothing
Set File2 = Nothing
Set File3 = Nothing
Set File4 = Nothing
Set File5 = Nothing

Function AddTextFileToOtherTextFile(ByVal strSourceFile, ByVal strDestinationFile, ByVal LineNum)

 Dim TS, tempStr, tempStr2(), i

 ReDim tempStr2(3)

 Set TS = FSO.OpenTextFile(strSourceFile, 1)

 tempStr = TS.ReadLine

 TS.Close

 Set TS = FSO.OpenTextFile(strDestinationFile, 1)

 For i = 0 To 3

    tempStr2(i) = TS.ReadLine   (  I GET ERROR HERE )

 Next

 TS.Close

 tempStr2(LineNum - 1) = tempStr

 Set TS = FSO.OpenTextFile(strDestinationFile, 2)

 TS.Write Join(tempStr2, vbCrLf)

 TS.Close

 FSO.DeleteFile strSourceFile

 Set TS = Nothing
 Set tempStr = Nothing
 Set i = Nothing

End Function
0
 
mvidasCommented:
Ok, perhaps your file5 doesnt have 4 lines in it currently.. replace the line that errors out with:

  If TS.AtEndOfStream Then
   tempStr2(i) = ""
  Else
   tempStr2(i) = TS.ReadLine
  End  If

Which will fill file5 with blank lines where the file contents aren't already in it.
0
 
mvidasCommented:
Glad to help! Let me know if you need anything else.
0
 
craigs052998Author Commented:
I just added one to the ms-dos area that works off what we did here if you would like some more points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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