Solved

If a folder exist then delete it using vbscript

Posted on 2004-04-29
13
962 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

623 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