-
Dec 11th, 2005, 09:07 AM
#1
Thread Starter
Addicted Member
How to Rename/Delete .TXT file ??
how can a rename .txt file and delete it ?
-
Dec 11th, 2005, 09:15 AM
#2
Re: How to Rename/Delete .TXT file ??
VB Code:
'to rename file:
Private Sub Command1_Click()
On Error Resume Next
Name "c:\temp\test.txt" As "c:\temp\test1.txt"
End Sub
'to delete file:
Private Sub Command1_Click()
On Error Resume Next
Kill "c:\temp\test.txt"
End Sub
Note: you will need "proper" error handler in place to check for file existance first (perhaps using Dir() function).
-
Dec 11th, 2005, 10:18 AM
#3
Addicted Member
Re: How to Rename/Delete .TXT file ??
I'm not sure if this function is good practice or not, but I use it
VB Code:
If FileExists(App.Path & "\filename") Then Kill (App.Path & "\filename")
Private Function FileExists(ByVal File As String) As Boolean
Dim fLen As Integer
On Error Resume Next
fLen = Len(Dir$(File))
If Err Or fLen = 0 Then
FileExists = False
Else
FileExists = True
End If
End Function
Only use App.Path if you want to search for the file where your exe resides. Obviously ByVal or ByRef is a choice you will make.
-
Dec 11th, 2005, 11:29 AM
#4
Addicted Member
Re: How to Rename/Delete .TXT file ??
My method:
Code:
Dim CHKFL as string
' Check if folder exists, first.
If Dir("C:\Dir", vbDirectory) = vbNullString Then
Exit Sub
end if
' Check if file exists
CHKFL = Dir$("C:\Dir\File.exe")
if CHKFL = "File.exe" then
Kill("C:\Dir\File.exe")
end if
-
Dec 11th, 2005, 02:33 PM
#5
Re: How to Rename/Delete .TXT file ??
That won't kill the file, as you aren't checking to see if CHKFL="C:\Dir\File.exe"
-
Dec 11th, 2005, 02:52 PM
#6
Addicted Member
Re: How to Rename/Delete .TXT file ??
Weell... You're the expert, but I've always used this code myself (VB6) and tested it with both the file existing and not existing, and it performs perfectly for me. Actually, I learnt that syntax from my college textbook, which expresses it exactly like i've put there...
-
Dec 11th, 2005, 02:59 PM
#7
Re: How to Rename/Delete .TXT file ??
Oops
I just tried it again, and you're right. I don't know what I was thinking.
Sorry.
-
Dec 11th, 2005, 03:43 PM
#8
Addicted Member
Re: How to Rename/Delete .TXT file ??
No problem ^_^ I cant talk anyway.. I'm a real noob at VB, but thanks to the lovely people here on the forums (Actually, you and Joacim are my biggest heros XD) I'm slowly learning more. I thank you for that
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|