DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Visual Basic 4 / 5 / 6 (http://www.daniweb.com/forums/forum4.html)
-   -   Need to delete a file (http://www.daniweb.com/forums/thread167571.html)

sumit-saini Jan 10th, 2009 5:18 pm
Need to delete a file
 
i want to delete a file from a specific location, if file Existing ; using JavaScript or VB scripting

~s.o.s~ Jan 11th, 2009 1:39 am
Re: Need to delete a file
 
Javascript, at least broswer embedded Javascript can't do this in a standard compliant way; moving to VB section.

Comatose Jan 11th, 2009 1:57 am
Re: Need to delete a file
 
Well, using VB6 it's really easy:
if dir("c:\autoexec.bat", vbnormal) <> vbnullstring then kill "c:\autoexec.bat". However, this becomes a little different in vbscript (and for some reason, a bit uglier).
dim filesys
Set filesys = WScript.CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\somefile.txt") Then
  filesys.DeleteFile "c:\somefile.txt"
End If

QVeen72 Jan 12th, 2009 3:11 am
Re: Need to delete a file
 
Hi,
use KILL:

If Dir("C:\MyText.txt") <> "" Then
    Kill "C:\MyText.txt"
End If

Regards
Veena

Comatose Jan 12th, 2009 9:46 am
Re: Need to delete a file
 
In VBScript, Kill won't work. I don't think dir() works in VBScript either. If doing this in VB6 (compiled language) it is strongly recommended that you do NOT compare the result of dir with "". if dir("C:\MyText.txt") <> vbnullstring then is a much better method. Double quotes is what is known as a null string. It uses somewhere in the area of around 8 bytes. vbnullstring is a special constant that is built into VB6, that reduces the memory usage to like 1 byte. This isn't a very big deal for small apps with a couple of strings.... but when you get into recursion, or loops with string processing and checking, it will make a visible difference.


All times are GMT -4. The time now is 11:18 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC