how can a rename .txt file and delete it ?
Printable View
how can a rename .txt file and delete it ?
Note: you will need "proper" error handler in place to check for file existance first (perhaps using Dir() function).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
I'm not sure if this function is good practice or not, but I use it :D
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.
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
That won't kill the file, as you aren't checking to see if CHKFL="C:\Dir\File.exe"
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...
Oops :blush:
I just tried it again, and you're right. I don't know what I was thinking.
Sorry.
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 :)