Link to home
Start Free TrialLog in
Avatar of jtrapat1
jtrapat1

asked on

Deleting A Text File From VB Application

I'm using VB6 against an SQL Server 7 database.
I'm trying to delete a text file and I'm using the Kill command.
I tried a lot of different syntaxes from MSDN, etc. but I still get an error: "File Not Found"
I shouldn't have any permissions problems.
I'm just trying to delete a text file from my C:\ drive.
ex:  Kill "c:\test.txt"

What am I doing wrong?
Or, is there another way to delete a text file from a vb program?

Thanks in Advance
John
ASKER CERTIFIED SOLUTION
Avatar of Crin
Crin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crin
Crin

As an additional suggestion I assume you use filename stored to variable, before deleting add
MsgBox FileName
to look what is actually placed there.

Sincerely,
Crin
You could also use the FileSystemObject to delete the file.

'Set reference to Microsoft Scripting RunTime

DeleteFile "C:\test.txt"

Private Sub DeleteFile(ByVal sFilePath As String)

Dim oFso As New FileSystemObject
If oFso.FileExists(sFilePath) Then
    oFso.DeleteFile(sFilePath)
End If

Set oFso = Nothing

End Sub
Avatar of Richie_Simonetti
Just a question: why all programmers are in love with a monstruosity like FSO to do really a simple job like delete a file?
It's like kill a fly with a machine gun :)
its an alternat solution is all Richie.

I agree that using kill is more efficient.  However, he is having a problem with it because the file doens't exist at that specified path.  So I just presented an alternate solution.
Is the VB program running locally or on the server?
Is the file on the C: local or on the server?

============================
If the VB program is running on a system other than the one where the file (to be deleted) resides, you will need more than just a simple Kill command.  For that matter, you will need more than just the FSO object.

Example:
VB program running locally needs a shared access to the root directory of the server.  Then the VB program can issue a command to delete the file:
Kill "\\NameOfServer\C$\test.txt"
where C$ is a file share name mapped to C:\ on the server.

Alternatively, you could invoke a stored procedure (through ADO) on the server that would deleted the file.
Hi
Try the following:

Dim objFile as FileSystemObject
Dim strFileLocation as String 'holds the file path
Dim strFileName as String 'holds the file name

objFile.Deletefile StrFileLocation, strFileName, true

Hope it works (does on my side)

Carl
jtrapat1,

Now that an answer has been proposed, you are likely to get very few new comments on solutions to your problem. If you we've solved your problem, please select the best comment or answer.  Otherwise, reject the proposed answer and respond to my questions.
Hi Iknownothing, welcome to EE. Please, before post a comment/answer read the guidelines at bottom of this page.
by the way, don't post an "answer" that was already posted by other participant.
Cheers
Avatar of jtrapat1

ASKER

Decided to go with another proposed solution.
My apologies to my over zealousness. Although I have asked a few questions in the past, this is the first answer I decided to supply. Sorry guys!

And especially Sorry to raizon, I did not realise you already gave the answer before I did.

Carl
no worries Iknownothing.

Common practice when posting is to post as a comment.  This will allow the questioner to accept the answer that best fits their needs.