• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

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
0
jtrapat1
Asked:
jtrapat1
  • 3
  • 2
  • 2
  • +3
1 Solution
 
CrinCommented:
You do exactly needed action, previously you can check if file is actually there with

Res = Dir("c:\test.txt")
if res <> "" then
  Kill "c:\test.txt"
end if

It *must* work if file is actually in the c:\path...

Sincerely,
Crin

0
 
CrinCommented:
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
0
 
raizonCommented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Richie_SimonettiIT OperationsCommented:
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 :)
0
 
raizonCommented:
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.
0
 
aikimarkCommented:
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.
0
 
IknownothingCommented:
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
0
 
aikimarkCommented:
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.
0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
jtrapat1Author Commented:
Decided to go with another proposed solution.
0
 
IknownothingCommented:
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
0
 
raizonCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now