Solved

If a folder exist then delete it using vbscript

Posted on 2004-04-29
13
961 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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 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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

710 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