Solved

VBS Script

Posted on 2006-06-20
12
280 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
[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
  • 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
Independent Software Vendors: 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!

 
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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

734 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