i want to delete a file from a specific location, if file Existing ; using JavaScript or VB scripting

Recommended Answers

All 4 Replies

Javascript, at least broswer embedded Javascript can't do this in a standard compliant way; moving to VB section.

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

Hi,
use KILL:

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

Regards
Veena

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.