Solved

VBS Script

Posted on 2006-06-20
12
275 Views
Last Modified: 2010-04-07
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
Comment
Question by:craigs052998
  • 7
  • 5
12 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 16944715
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
 
LVL 35

Expert Comment

by:mvidas
ID: 16944726
My apologies, the "Set FSO = CreateObject..." line should be after the "File5 = ..." line, not within the function.  Very sorry about that.
0
 

Author Comment

by:craigs052998
ID: 16944734
Will this append?  I only want 4 line to ever appear in the main file?
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16944840
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
 

Author Comment

by:craigs052998
ID: 16944866
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
 
LVL 35

Expert Comment

by:mvidas
ID: 16944904
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:craigs052998
ID: 16944929
BINGO, right on the head
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16944977
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
 

Author Comment

by:craigs052998
ID: 16945164
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
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 16945218
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
 
LVL 35

Expert Comment

by:mvidas
ID: 16955133
Glad to help! Let me know if you need anything else.
0
 

Author Comment

by:craigs052998
ID: 16955164
I just added one to the ms-dos area that works off what we did here if you would like some more points
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now