Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to combine multiple vbs scripts into 1 script?

Status
Not open for further replies.

stevemarks59

Technical User
Apr 8, 2010
22
US
How can I combine these 10 vbs files into 1 vbs file and include "On Error Resume Next"?

To delete these folders:

C:\Profiles\STEVE\extensions
D:\Profiles\STEVE\extensions

I use the following 2 scripts:

CODE
Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FolderExists(pathToFolder)) Then
Set objFolder = objFS.GetFolder(pathToFolder)
objFolder.Delete True
End if


CODE
pathToFolder = ("D:\Profiles\STEVE\extensions")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FolderExists(pathToFolder)) Then
Set objFolder = objFS.GetFolder(pathToFolder)
objFolder.Delete True
End if



To delete these files:

C:\Profiles\STEVE\extensions.rdf
C:\Profiles\STEVE\extensions.ini
C:\Profiles\STEVE\extensions.cache
C:\Profiles\STEVE\extensions.log
D:\Profiles\STEVE\extensions.rdf
D:\Profiles\STEVE\extensions.ini
D:\Profiles\STEVE\extensions.cache
D:\Profiles\STEVE\extensions.log

I use the following 8 scripts:


CODE
pathToFile = ("C:\Profiles\STEVE\extensions.rdf")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("C:\Profiles\STEVE\extensions.ini")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("C:\Profiles\STEVE\extensions.cache")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("C:\Profiles\STEVE\extensions.log")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("D:\Profiles\STEVE\extensions.rdf")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("D:\Profiles\STEVE\extensions.ini")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("D:\Profiles\STEVE\extensions.cache")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if


CODE
pathToFile = ("D:\Profiles\STEVE\extensions.log")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FileExists(pathToFile)) Then
Set objFile = objFS.GetFile(pathToFile)
objFile.Delete True
End if




I would appreciate help on this.
Thank you.
 
The first CODE above:

CODE
Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FolderExists(pathToFolder)) Then
Set objFolder = objFS.GetFolder(pathToFolder)
objFolder.Delete True
End if



Should have been:

CODE
pathToFolder = ("C:\Profiles\STEVE\extensions")

Set objFS = CreateObject("Scripting.FileSystemObject")
If (objFS.FolderExists(pathToFolder)) Then
Set objFolder = objFS.GetFolder(pathToFolder)
objFolder.Delete True
End if


Sorry about that!
 
I was given the answer on another forum.
Here is the link:
Here is the script:

CODE:
-------------------------------------------------------
Dim objFS
Dim strFolders(1)
Dim strFiles(7)
Dim i

Set objFS = CreateObject("Scripting.FileSystemObject")

'Get a list of the folders
strFolders(0) = "C:\Profiles\STEVE\extensions"
strFolders(1) = "D:\Profiles\STEVE\extensions"
'Delete the folder if they exist
For i = 0 To 1
If objFS.FolderExists(strFolders(i)) Then
objFS.DeleteFolder strFolders(i), True
End If
Next

'Get a list of the files
strFiles(0) = "C:\Profiles\STEVE\extensions.rdf"
strFiles(1) = "C:\Profiles\STEVE\extensions.ini"
strFiles(2) = "C:\Profiles\STEVE\extensions.cache"
strFiles(3) = "C:\Profiles\STEVE\extensions.log"
strFiles(4) = "D:\Profiles\STEVE\extensions.rdf"
strFiles(5) = "D:\Profiles\STEVE\extensions.ini"
strFiles(6) = "D:\Profiles\STEVE\extensions.cache"
strFiles(7) = "D:\Profiles\STEVE\extensions.log"
'Delete the files if they exist
For i = 0 To 7
If objFS.FileExists(strFiles(i)) Then
objFS.DeleteFile strFiles(i), True
End If
Next

'Cleanup
Set objFS = Nothing

----------------------------------------------------------
 
Not trying to sound crabby here, but the VBScripts could really use a cleanup around here. All those Dim statements just clutter your script without an Option Explicit statement. WSH VBS automatically releases the memory, so there's no use for things like this:
set objFSO=nothing
 
perhaps suggest something constructive?

+ instead of For i = 0 To 7 and For i = 0 To 1, migth be more dynamic to have something like For i = 0 To UBound(strFiles)
+ perhaps the naming convention of your arrays being strFiles might be better as arrFiles?
+ perhaps some form of logging in your scripts would be helpful for audit / debuggin. simple Wscript.Echo's would be a good start
+ include Option Explicit
+ include error trapping around method calls which 'may' still error
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top