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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS to Search and Delete 4

Status
Not open for further replies.

dbustamante

IS-IT--Management
Jul 12, 2010
63
US
Hey all,

Ive been givin the task to come up with a VBS sript to search and delete all music files my users save to their computers. so ive tried coming up with a log on script that will search and delete all music files on their profile. im pretty new at script writing but this is what ive come up with so far.

Dim fso

fso.DeleteFile "C:\Documents and Settings\*current users*\desktop\*.MP3*", True

but when ever i run it i get an error message "bad name or number" does anybody see what im missing or where i went wrong? Also would it be possible to add .wma and .mp4 to this script or no?
 
*current users* will need to be replaced with something better. WshShell.ExpandEnvironmentStrings("%username%") may be a good place to start?
 
ok so i came up with this but im still erroring out should i add "and" after true?

Dim fso

fso.DeleteFile "C:\Documents and Settings\WshShell.ExpandEnvironmentStrings("%username%") \desktop\*.MP3*", True
objFSO.DeleteFile("C:\SomeDirectory\*.mp4")
objFSO.DeleteFile("C:\SomeDirectory\*.wma")
 
Try this:
strUser = WshShell.ExpandEnvironmentStrings("%username%")
fso.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.MP3*", True

Also note that if you use a wildcard in the delete command when there is no file that matches, an error will occur. You can ignore the error with the following:

On error resume next
...code to delete files
on error goto 0
 
ok i tried this one and now im getting an error "object required: "wshshell""

strUser = WshShell.ExpandEnvironmentStrings("%username%")
fso.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.MP3*", True
On error resume next
code to delete files
on error goto=0
 
The WshShell object needs to be declared (if you are using option explicit) and created.

Add this to the beginning of your script:
Code:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
 
ok i added it to my script and now i have

Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strUser = WshShell.ExpandEnvironmentStrings("%username%")
fso.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.mp3*", True


and now im getting an error "Object required :'fso'"
 
That means you forgot to declare and create the FileSystemObject. Add the following to the beginning of your script:

Code:
Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
ok so heres what i came up with useing what you gave me

Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strUser = WshShell.ExpandEnvironmentStrings("%username%")
fso.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.mp3*", True

it seems like were almost there i error out on line 6 Char 1 object Required: " but im looking at line 6 and it doesnt look like its missing anything.
 
[!]objFSO[/!].DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.mp3*", True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Are you sure there is at least 1 .mp3 file there to delete?

Refer back to my post timestamped: 13 Jul 10 11:34
 
i changed the file type to .wma and it still comes out with Error no files found and those are the only audio files i have.

Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strUser = WshShell.ExpandEnvironmentStrings("%username%")
objFSO.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.wma*", True
On error resume next
objFSO.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.wma*", True
on error goto 0
 
I removed the first delete command and it seems to work.
Code:
Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strUser = WshShell.ExpandEnvironmentStrings("%username%")
On error resume next
objFSO.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.wma", True
on error goto 0
 
ok the error messages stoped but the files .wma files that i have are still there. should i add another delete command somewhere? also will "dim fso" work over group policy?

Dim fso
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
strUser = WshShell.ExpandEnvironmentStrings("%username%")
On error resume next
objFSO.DeleteFile "C:\Documents and Settings\" & strUser & "\desktop\*.wma*", True
on error goto 0
 
Two things;

First, the "username" may not match the user's profile folder. For example, my username might be jsmith, but if I'm on the ABC domain, the path to my desktop could be:
C:\Documents and Settings\jsmith.ABC\desktop
or
C:\Documents and Settings\jsmith.ABC.000\desktop
etc.

The "userprofile" environment variable would be more accurate, as in:
Code:
strUserProfile = WshShell.ExpandEnvironmentStrings("%userprofile%")
On error resume next
objFSO.DeleteFile strUserProfile & "\desktop\*.wma*", True
on error goto 0

More importantly, "on error resume next" suppresses error messages, which is fine... as long as you check the error value immediately after executing the command, and handle errors accordingly. Otherwise, you don't know what's going on. For example:

Code:
On error resume next
objFSO.DeleteFile strUserProfile & "\desktop\*.wma*", True
[COLOR=blue]if err.number <> 0 then 
   wscript.echo "Got error#" & err.number & ": " & err.description
End If[/color]
on error goto 0

So putting it all together, try the code below. Still could use some more tweaking, but this should get you going:
Code:
Option Explicit
Dim objFSO, WshShell, strUserProfile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strUserProfile = WshShell.ExpandEnvironmentStrings("%userprofile%")

On Error Resume Next
objFSO.DeleteFile strUserProfile & "\desktop\*.wma*", True
If Err.Number <> 0 then 
   Wscript.Echo "Got error#" & Err.Number & ": " & Err.Description
End If
On Error Goto 0
 
there we go we got it if i wanted to add .mp3 .mp4 to it would it look something like this?

Option Explicit
Dim objFSO, WshShell, strUserProfile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strUserProfile = WshShell.ExpandEnvironmentStrings("%userprofile%")

On Error Resume Next
objFSO.DeleteFile strUserProfile & "\desktop\*.wma*", True
objFSO.DeleteFile strUserProfile & "\desktop\*.mp3*", True
objFSO.DeleteFile strUserProfile & "\desktop\*.mp4*", True
If Err.Number <> 0 then
Wscript.Echo "Got error#" & Err.Number & ": " & Err.Description
End If
On Error Goto 0
 
dbustamante, i think you post should have looked more like:

'thanks for that post PHV, i have added the objFSPO'
'thanks for that post guitarzan, that sets it out clearly for me now, i have a better understanding'.

a star for the helpful posts might be a nice thing to do.

instead, of the above, you again ask for more assistance with regards the .mp3 and .mp4 stuff.

to be frank, you have posted 'suggested' code for how to also remove .mp3 and .mp4 files. why dont you just test your own code on your machine and see if it works successfully?
 
but, no, your use of If Err.Number <> 0 is the correct syntax but will not give you the desired result
 
mrmovie, while i have not yet given praise to jges, PHV, and guitarzan for their time that does not mean i am not grateful and thankful i am simply very eager to learn more because with what they have showed me i really do have a better understanding of what scripting should look like, And the best way to learn is to ask questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top