Solved

Deleting A Text File From VB Application

Posted on 2002-05-29
12
150 Views
Last Modified: 2011-09-20
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
Comment
Question by:jtrapat1
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 2

Accepted Solution

by:
Crin earned 50 total points
ID: 7043052
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
 
LVL 2

Expert Comment

by:Crin
ID: 7043059
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
 
LVL 5

Expert Comment

by:raizon
ID: 7043109
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7043152
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
 
LVL 5

Expert Comment

by:raizon
ID: 7043231
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
 
LVL 45

Expert Comment

by:aikimark
ID: 7043608
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:Iknownothing
ID: 7043881
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
 
LVL 45

Expert Comment

by:aikimark
ID: 7044342
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7044460
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
 

Author Comment

by:jtrapat1
ID: 7044483
Decided to go with another proposed solution.
0
 

Expert Comment

by:Iknownothing
ID: 7044488
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
 
LVL 5

Expert Comment

by:raizon
ID: 7044493
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now