Add .LOG to .txt file on creation

P

Piranha

Hi,
In using this code to create and copy data to a .txt file.

I want to (on the inital creation of the file ONLY) insert .LOG

as the first line.


Code:
--------------------
Sub TxtFile()
Dim rrr
Dim MyTest99
Dim MyData
rrr = FreeFile
MyData = Selection

'On only the inital creation of the txt file, I want .LOG inserted

MyTest99 = "E:\backup & testing\test99.txt"
Open MyTest99 For Append Shared As #rrr
Print #rrr, MyData
Close #rrr
End Sub
 
L

Leith Ross

Hello Piranha,


Code:
--------------------

Sub TxtFile()
Dim rrr
Dim MyTest99
Dim MyData
rrr = FreeFile
MyData = Selection

'On only the inital creation of the txt file, I want .LOG inserted
MyTest99 = "E:\backup & testing\test99"

'Test if .log file exists - Append will create the file if it doesn't exist
If Dir(MyTest99 & ".log") Then
Name MyTest & ".log" As MyTest99 & ".txt"
MyTest99 = MyTest99 & ".txt"
Else
MyTest99 = MyTest99 & ".log"
End If

Open MyTest99 For Append Shared As #rrr
Print #rrr, MyData
Close #rrr
End Sub
 
L

Leith Ross

Hello Piranha,


Code
-------------------

Sub TxtFile()
Dim rrr
Dim MyTest99
Dim MyData
rrr = FreeFile
MyData = Selection

'On only the inital creation of the txt file, I want .LOG inserted
MyTest99 = "E:\backup & testing\test99"

'Test if .log file exists - Append will create the file if it doesn't exist
If Dir(MyTest99 & ".log") Then
Name MyTest & ".log" As MyTest99 & ".txt"
MyTest99 = MyTest99 & ".txt"
Else
MyTest99 = MyTest99 & ".log"
End If

Open MyTest99 For Append Shared As #rrr
Print #rrr, MyData
Close #rrr
End Sub
 
D

Dave Peterson

I read your post slightly differently. You want .LOG inserted as the first line
of a .txt file when it is created?

Option Explicit
Sub TxtFile()
Dim rrr As Long
Dim MyTest99 As String
Dim MyData As String
Dim testStr As String

rrr = FreeFile
MyData = Selection.Cells(1).Value
MyTest99 = "E:\backup & testing\test99.txt"

'assumes that the drive/folder exists
testStr = ""
On Error Resume Next
testStr = Dir(MyTest99)
On Error GoTo 0

Open MyTest99 For Append As #rrr
If testStr = "" Then
Print #rrr, ".LOG"
End If
Print #rrr, MyData
Close #rrr
End Sub


A neat way to keep track of time/dates in Text files.

(A hidden feature with notepad if anyone wants to see what it does.)
 
P

Piranha

Hi Leith & Dave,

Leith, I can't get past this line with your code.

Code:
--------------------
If Dir(MyTest99 & ".log") Then
--------------------
I am going to keep
playing with it though

Dave, Exactly what i want. I am going to make the selection
part, to copy the entire selection instead of one cell and i will be
done.

Thx guys, very much appricated.
 
L

Leith Ross

Hello Piranha,

Looks like I got it wrong. Sorry. What's the question about my line o
code?

Sincerely,
Leith Ros
 
P

Piranha

Leith said:
Hello Piranha,

Looks like I got it wrong. Sorry. What's the question about my line of
code?

Sincerely,
Leith RossHi Leith,
I get a 'Type Mismatch' on this line.

Code:
 
L

Leith Ross

Hello Piranha,

I am striking out with you. I didn't correct that line like I thought I
had when I posted it. It should be...

If Dir(MyTest99 & ".log") <> ""Then

The purpose is check if the file has a ".log" extension (first time it
was created). I mistakenly thought you want the file to have a ".log"
extension only after it was first created. After being accessed the
second time to then change the ".log" extension to ".txt" (presumably
as a measure of security - so I thought). Anyway, the code may not be a
total loss if you can get something useful from it.

Sincerely,
Leith Ross
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top