Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Deleting A Text File From VB Application

Posted on 2002-05-29
12
Medium Priority
?
159 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 46

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
 

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 46

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

877 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