Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBS Script

Posted on 2006-06-20
12
Medium Priority
?
283 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
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!

 
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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

926 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