Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Text in a Text File

Status
Not open for further replies.

kupz

Technical User
Aug 23, 2001
24
US
I am new to VB scripting but know that this problem of mine can be solved using this languauge but just can't figure it out. I am using VBA to export text files from more than one Access database to a directory. These files are used to load another application since it is a file-based system. My problem is inserting text programmatically in these Access generated files. It is very tedious to insert every time these files are generated since you have to insert, save, and exit the file. Three rows of text are inserted only at the top of each file. Any suggestions for doing this in VBScipt. Thanks for your help.
 
myniguez,

Since you already know what the three lines at the top of the file are, open a new file, write the three lines, then open and append the Access generated file to the new one. You cannot prepend new lines to a file or add lines at the beginning of a file. So what you are doing here is to create the new file and add existing lines.

Cheers,
fengshui_1998
 
fengshui_1998,

Thanks for the advice. I think that is a good idea. Do you think you can be me some sample code on how to go about this? Thanks again!

myniguez
 

Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")

' Open existing file
Set infile = fso_OpenTextFile("C:\X.txt", ForReading, True)
'
' Open a new file and write the new lines
Set newfile = fso_OpenTextFile("C:\New.txt", ForWriting, True)
newfile.writeline "Here is line 1"
newfile.writeline "Here is line 2"
newfile.writeline "Here is line 3"

' Read the lines from the input file and write to new file
Do While infile.AtEndOfStream = FALSE
txt = infile.ReadLine
newfile.writeline txt
Loop
infile.close
newfile.close


Cheers,
fengshui_1998
 
fengshui_1998,

Thanks for the advice. That's a good idea. Do you think you can give me some sample code so I can do this? Thanks for your help!
 
fengshui_1998,

This is great!! I can't thank you enough. I've automated all my tasks at work. Maybe I can sit back now and relax at month-end....don't think so.

Thanks again!

 
fengshui_1998,

I have a small problem here. I've done all the coding to automate my tasks but I just realized that every month-end I will still have to change the second and third line of text. Is there a way around this? Thanks for the help again!

 
myniguez,

Add this code to check for last day of month

If LDOM(date) then
newfile.writeline "Here is end-of-month line 2"
newfile.writeline "Here is end-of-month line 3"
Else
newfile.writeline "Here is line 2"
newfile.writeline "Here is line 3"
End If


' Add this code to the bottom of your vbscript.
' ***********************
Function LDOM(tmpDate)
' ***********************
'
' Returns TRUE if tmpDate is last day of the month
' Date formats: 23/2/2000 or 23-February-2000
Dim months, Lastday

strMonths = "January,February,March,April,May,June,July,August,September,October,November,December"
months = split(strMonths, ",")

strLastDays = "31,28,31,30,31,30,31,31,30,31,30,31"
Lastday = split(strLastDays, ",")
'
' First get the day, month, and year from the given date

mm = Month(tmpDate)
dd = Day(tmpDate)
yy = Year(tmpDate)
'
' Get the last day of the month for this year

' First, determine if this is a leap year
If LeapYearCheck(yy) then
LastDay(1) = 29
Else
LastDay(1) = 28
End if
'
If int( Lastday(mm-1) ) = int(dd) then
LDOM = True
Else
LDOM = False
End IF

End Function

' ******************************
Function LeapYearCheck(testyear)
' ******************************

' Years not evenly divisible by 4 are not leap years
' OR
' Years evenly divisible by 100 but not evenly divisible by 400 are not leap years

If (testyear mod 4) <> 0 or ( (testyear mod 100 = 0) and (testyear mod 400 <> 0) ) then
leapyearCheck = False
Else
leapyearCheck = True
End If

End Function


fengshui_1998
 
fengshui_1998,

You're a saviour. I will try doing this and add to what I already have. I actually just posted another message in the message board and I was just thinking of doing a DSN connection and getting the value from a period table since it contains the current month-end value. But I think I will use what you gave me. I think this will solve this dilemma. I appreciate all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top