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
Solved

If a folder exist then delete it using vbscript

Posted on 2004-04-29
13
956 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
clicking a shape in a frame array vb6 3 47
VB 6 error 5 in windows 10 but not in XP 7 63
Advice in Xamarin 21 97
IF ELSE Statement in Excel Macro VBA 16 75
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…
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…
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…
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…

829 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