ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Add .LOG to .txt file on creation (https://www.excelbanter.com/excel-programming/344525-add-log-txt-file-creation.html)

Piranha[_58_]

Add .LOG to .txt file on creation
 

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
--------------------

Any direction appreciated.
Dave


--
Piranha
------------------------------------------------------------------------
Piranha's Profile: http://www.excelforum.com/member.php...o&userid=20435
View this thread: http://www.excelforum.com/showthread...hreadid=480975


Leith Ross[_188_]

Add .LOG to .txt file on creation
 

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

--------------------

Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=480975


Leith Ross[_197_]

Add .LOG to .txt file on creation
 

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

-------------------

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=48097


Dave Peterson

Add .LOG to .txt file on creation
 
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.)

Piranha wrote:

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
--------------------

Any direction appreciated.
Dave

--
Piranha
------------------------------------------------------------------------
Piranha's Profile: http://www.excelforum.com/member.php...o&userid=20435
View this thread: http://www.excelforum.com/showthread...hreadid=480975


--

Dave Peterson

Piranha[_60_]

Add .LOG to .txt file on creation
 

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.
Dave Peterson Wrote:
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.)

Piranha wrote:

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
--------------------

Any direction appreciated.
Dave

--
Piranha

------------------------------------------------------------------------
Piranha's Profile:

http://www.excelforum.com/member.php...o&userid=20435
View this thread:

http://www.excelforum.com/showthread...hreadid=480975

--

Dave Peterson



--
Piranha
------------------------------------------------------------------------
Piranha's Profile: http://www.excelforum.com/member.php...o&userid=20435
View this thread: http://www.excelforum.com/showthread...hreadid=480975


Leith Ross[_221_]

Add .LOG to .txt file on creation
 

Hello Piranha,

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

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=48097


Piranha[_61_]

Add .LOG to .txt file on creation
 

Leith Ross Wrote:
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:
--------------------
If Dir(MyTest99 & ".log") Then
--------------------
Dave


--
Piranha
------------------------------------------------------------------------
Piranha's Profile: http://www.excelforum.com/member.php...o&userid=20435
View this thread: http://www.excelforum.com/showthread...hreadid=480975


Leith Ross[_223_]

Add .LOG to .txt file on creation
 

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


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=480975



All times are GMT +1. The time now is 12:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com