Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

If a folder exist then delete it using vbscript

Posted on 2004-04-29
13
Medium Priority
?
964 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 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

730 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