Solved

If a folder exist then delete it using vbscript

Posted on 2004-04-29
13
948 Views
Last Modified: 2007-12-19
I have written a vbscript to delete certain files in the start menu.  This works fine the 1st time somebody logs-in but the second time they login they receive a vbscript error saying the file does not exist (Which is correct),  Because I am a beginner in vbscript I would like to know how to set the script to say "if this file exist then delete it, if it not continue through the script" (without giving an error "file does not exist").
0
Comment
Question by:GRCHELPDESK
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 9

Expert Comment

by:gilbar
ID: 10953059
on error resume next

that line will skip any line that would make an eror
0
 
LVL 9

Accepted Solution

by:
gilbar earned 125 total points
ID: 10953091
or use an if statement that looks at the results of a dir statement:

If Len(Dir("c:\test1.doc")) > 0 Then
'delete "c:\test1.doc"
End If
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10953103
Dim oFSO,strFld
set oFSO = CreateObject("Scripting.FileSystemObject")
strFld = "C:\SomeFolder"

If oFSO.FolderExists(strFld) Then
MsgBox "file found"
Else
MsgBox "File not found"
End If
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10953122
the above was to check for a folder,for a file use FileExists

Dim oFSO,strFile
set oFSO = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Somefile.txt"

If oFSO.FileExists(strFile) Then
MsgBox "file found"
Else
MsgBox "File not found"
End If
0
 
LVL 4

Expert Comment

by:Javin007
ID: 10953128
I'd recommend GilBar's suggestion with only a minor change:

If Dir$(FileName) <> "" Then Kill FileName

-Javin
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10953159
I don't think you can use the $ in vbscript,can you?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:gilbar
ID: 10953160
yes jav, i like your code better  a) it's more compacter  and b) it has a command that will actually delete something ( i was experiencing a brain lockup and couldn't remember how to delete  :)  
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10953232
Dim oFSO,strFile
set oFSO = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Somefile.txt"

If oFSO.FileExists(strFile) Then oFSO.DeleteFile strFile
0
 
LVL 4

Expert Comment

by:Javin007
ID: 10954578
"I don't think you can use the $ in vbscript,can you?"

If it's anything like VB, then not only can you, but you should.

The $ tells VB that it's going to be returning a string.  If you don't use it, VB will return a variant, then have to go through the extra steps of converting the variant to a string before it can be stored in your string variable.  This should be used for all functions that return strings, such as Mid$() Chr$() UCase$() LCase$() Trim$() Left$() Right$(), etc. etc. etc.

-Javin
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10955152
Yes I know,I use them all the time. But as far as I am aware the string versions are not available in vbscript.To see what I mean try adding this to a textfile and rename with a .vbs extension

Dim fss
fss = "  My String  "
MsgBox Trim(fss)

That will work.Then try this:

Dim fss
fss = "  My String  "
MsgBox Trim$(fss)

and it will give an error.
0
 
LVL 4

Expert Comment

by:Javin007
ID: 10955528
Ah, interesting to know!

-Javin
0
 
LVL 2

Author Comment

by:GRCHELPDESK
ID: 10961590
Great...thanks for your help.  Works like a charm.]

-Shawn
0
 
LVL 9

Expert Comment

by:gilbar
ID: 10962134
glad to help!  
0

Featured Post

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!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

12 Experts available now in Live!

Get 1:1 Help Now