Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


desktop deleting some file script

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,25667
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> desktop deleting some file script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 desktop deleting some file script - 9/7/2005 7:21:16 PM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
What I'm trying to do is delete certain files off user's desktop & create a shortcut to their desktop within a login script. This is what i have but seems to be missing a object of some sort. The shortcut seems to be working but the deletion isn't


Dim fso, oFile
Set Shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\paper.lnk")
link.TargetPath = "\\server1\mo\My Documents\My Music\paper.xls"
link.Save
DesktopPath = Shell.SpecialFolders("Desktop")
If HCase(oFile.Name) = "PAPER.XLS" _
Or LCase(oFile.Name) = "Shortcut to PAPER" _
Or LCase(oFile.Name) = "PAPER" Then
fso.DeleteFile oFile.Path, True
end if
 
 
Post #: 1
 
 RE: desktop deleting some file script - 9/7/2005 7:37:53 PM   
  Zifter


Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
To delete the files from the desktop, you will have to get a files collection containing all the files on the desktop, check each file's name and delete it if it matches your criteria OR if you know the name of the file(s) to be deleted, you can bind directly to the file and delete it.

Example1


Option Explicit

Dim objFso
Dim objShell
Dim objFolder
Dim colFiles
Dim objFile
Dim strPathToDesktop

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.shell")

strPathToDesktop = objShell.SpecialFolders("Desktop")
Set objFolder = objFso.GetFolder(strPathToDesktop)
Set colFiles = objFolder.Files

For Each objFile In colFiles
If UCase(objFile.Name) = "PAPER.XLS" Or _
  UCase(objFile.Name) = "SHORTCUT TO PAPER" Or _
  UCase(objFile.Name) = "PAPER" Then
objFso.DeleteFile objFile.Path,True
End If
Next

Set colFiles = Nothing
Set objFolder = Nothing
Set objShell = Nothing
Set objFso = Nothing




Example2


Option Explicit

Dim objFso
Dim objShell
Dim objFile
Dim strPathToDesktop

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.shell")

strPathToDesktop = objShell.SpecialFolders("Desktop")
Set objFile = objFso.GetFile(strPathToDesktop & "\PAPER.XLS")
objFile.Delete

Set objFile = Nothing
Set objShell = Nothing
Set objFso = Nothing




Example3


Option Explicit

Dim objFso
Dim objShell
Dim strPathToDesktop

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.shell")

strPathToDesktop = objShell.SpecialFolders("Desktop")
objFso.DeleteFile strPathToDesktop & "\PAPER.XLS"

Set objShell = Nothing
Set objFso = Nothing







HTH



remarks:
- there doesn't exist a function named HCase() I think you wanted to use UCase, which converts a string to uppercase
- if you use the function LCase, which converts a string to lower case, you will never have a match if the other string is in uppercase. LCase(objFile.Name) = "PAPER.XLS" will never find a match, you should use it like this: LCase(objFile.Name) = "paper.xls"

< Message edited by Zifter -- 9/7/2005 7:45:53 PM >

(in reply to kracksmith)
 
 
Post #: 2
 
 RE: desktop deleting some file script - 9/7/2005 7:51:23 PM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
Thanks

i'll try out example 1 script tomorrow


why are these set to nothing

Set colFiles = Nothing
Set objFolder = Nothing
Set objShell = Nothing
Set objFso = Nothing



yes i meant UCASE instead of HCASE. but what happens if the file has upper and lower case? "Paper". should i use the UCASE only since it has only 1 upper case letter?

(in reply to Zifter)
 
 
Post #: 3
 
 RE: desktop deleting some file script - 9/7/2005 8:24:18 PM   
  Zifter


Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
In the beginning of the script the objects are created. Then the objects are used. And finally the objects are destroyed again (by setting them to Nothing). If we don't specifically destroy the objects, they remain in memory with the risk of interfering with other scripts or maybe even use all available memory (which isn't very likely to happen, but theoretically it can)


The functions UCase() and LCase() convert a string into respectively uppercase and lower case, it doesn't matter what case the original string is in.

Wscript.Echo UCase("tHIs iS A TESt sTRinG")
Output: THIS IS A TEST STRING

Wscript.Echo LCase("tHIs iS A TESt sTRinG")
Output: this is a test string


HTH

(in reply to kracksmith)
 
 
Post #: 4
 
 RE: desktop deleting some file script - 9/8/2005 1:50:41 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
hmmm, i tried these scripts and it does look like what it's suppose to do but can't get it to work.

these files i'm trying to delete are actually shortcuts users created.


I even added the shortcut extension "ink" to be deleted but still says can't find file to delete.


(in reply to Zifter)
 
 
Post #: 5
 
 RE: desktop deleting some file script - 9/8/2005 1:55:30 AM   
  Zifter


Posts: 318
Score: 0
Joined: 1/5/2005
From: Belgium
Status: offline
Can you post the script, maybe it's just some small typing mistake...

(in reply to kracksmith)
 
 
Post #: 6
 
 RE: desktop deleting some file script - 9/8/2005 7:37:22 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
The script that didn't work for me was your Example 1 script, except I changed the names for the deletion.

I have a new desktop deletion script that is working but now the copy desktop shortcut isn't working which is integrated in the same script.

Any ideas what's wrong with this scripting of why won't it create a shortcut to the desktop?



Option Explicit
Dim oFS
Dim oWsh
Dim oNetwork
Dim strUser
Set oWsh = CreateObject("WScript.Shell")
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Set oNetwork= CreateObject("Wscript.Network")
Set Shell = CreateObject("WScript.Shell")

strUser = oNetwork.UserName

DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\Paper List.lnk")
link.TargetPath = \\Server1\Local Drive\Storage\house\paper.xls
link.Save

Dim icon1
icon1 = oWsh.SpecialFolders("Desktop") & "\PAPER.XLS.lnk"
Dim icon2
icon2 = oWsh.SpecialFolders("Desktop") & "\Shortcut to PAPER.lnk"
Dim icon3
icon3 = oWsh.SpecialFolders("Desktop") & "\PAPER.lnk"
Dim icon4
icon4 = oWsh.SpecialFolders("Desktop") & "\ABC PAPER LIST.XLS.lnk"
IF (oFS.FileExists(icon1)) then
   oFS.deletefile (icon1)
End if
IF (oFS.FileExists(icon2)) then
   oFS.deletefile (icon2)
End if
IF (oFS.FileExists(icon3)) then
   oFS.deletefile (icon3)
End if
IF (oFS.FileExists(icon4)) then
   oFS.deletefile (icon4)
End if
Set oFS = Nothing
WScript.Quit (0)

(in reply to Zifter)
 
 
Post #: 7
 
 RE: desktop deleting some file script - 9/8/2005 7:44:46 AM   
  ebgreen


Posts: 5069
Score: 31
Joined: 7/12/2005
Status: online
This line should fail due to lack of quotes:

link.TargetPath = \\Server1\Local Drive\Storage\house\paper.xls


Do you have On Error Resume next in your script? If so, remove it to see what the errors are.

(in reply to kracksmith)
 
 
Post #: 8
 
 RE: desktop deleting some file script - 9/8/2005 8:15:34 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
 
hmm, i need that statement. i just forgot to include the quotes

link.TargetPath = "\\Server1\Local Drive\Storage\house\paper.xls"    'this should be valid now


after i fixed the above statement with quotes there still seem to be an error with the "set Shell"

Set Shell = CreateObject("WScript.Shell")  'statement seems good to me but it fails at this point


(in reply to ebgreen)
 
 
Post #: 9
 
 RE: desktop deleting some file script - 9/8/2005 8:29:07 AM   
  ebgreen


Posts: 5069
Score: 31
Joined: 7/12/2005
Status: online
Are you doing this as a webpage/HTA, or a .vbs file?

(in reply to kracksmith)
 
 
Post #: 10
 
 RE: desktop deleting some file script - 9/8/2005 8:34:26 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
I'm doing VBS


Also if i rem out the Option Explicit, the desktop shortcut works but the deletion doesn't


What am I missing besides a class in VBS? :-)

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: desktop deleting some file script - 9/8/2005 11:26:35 AM   
  ebgreen


Posts: 5069
Score: 31
Joined: 7/12/2005
Status: online
What error does it give you?

(in reply to kracksmith)
 
 
Post #: 12
 
 RE: desktop deleting some file script - 9/8/2005 3:25:32 PM   
  binHEX


Posts: 9
Score: 1
Joined: 9/2/2005
Status: offline
When you put Option Explicit at the beginning of your script, one of the things it does is force you to DIM all of your variables, prior to using them.  And since you weren't DIM'ing all of your variables, it was erroring out on ya.  I reworked your script real fast, and it should work now, but I haven't tested it, so.... No Warranty, Express or Implied, , your mileage may vary, item may differ in appearance from the picture on the box, objects are closer than they appear, yadda yadda.... 
Instead of using IF...THEN statements to test for like 5 or 6 diff things one after another, try using SELECT CASE...CASE statements.  They run faster, and they look cooler - which is what is really important.  After all, we're geeks.  ANYTHING that we can do to look cooler is a Good Thing, right?

Option Explicit
Dim oFS, oWsh, oNetwork, strUser
Dim DesktopPath, link
Dim folder, file, icon1, icon2, icon3, icon4

Set oWsh = CreateObject("WScript.Shell")
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Set oNetwork= CreateObject("Wscript.Network")
Set Shell = CreateObject("WScript.Shell")

 
strUser = oNetwork.UserName
 
DesktopPath = oShell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\Paper List.lnk")
link.TargetPath = "
\\Server1\Local Drive\Storage\house\paper.xls"
link.Save


icon1 = UCase(oWsh.SpecialFolders("Desktop") & "\PAPER.XLS.lnk")
icon2 = UCase(oWsh.SpecialFolders("Desktop") & "\Shortcut to PAPER.lnk")
icon3 = UCase(oWsh.SpecialFolders("Desktop") & "\PAPER.lnk")
icon4 = UCase(oWsh.SpecialFolders("Desktop") & "\ABC PAPER LIST.XLS.lnk")

Set folder = oFS.GetFolder(DesktopPath)
For Each file In folder.Files
Select Case UCase(file)
Case icon1
 oFS.DeleteFile(file)
Case icon2
 oFS.DeleteFile(file)
Case icon3
 oFS.DeleteFile(file)
Case icon4
 oFS.DeleteFile(file)
Case Else
End Select
Next
WScript.Quit (0)


Have fun!
binHEX 

< Message edited by binHEX -- 9/8/2005 3:30:33 PM >

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: desktop deleting some file script - 9/9/2005 2:32:14 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
Thanks for your input and it does look better too!


My last script with the IF statements has an error that says "shell" isn't defined.  well it is define "Set Shell = CreateObject("WScript.Shell")"
 
 
i just ran your script with the case statement and same thing.
 
 
what is causing the shell not being define?

(in reply to binHEX)
 
 
Post #: 14
 
 RE: desktop deleting some file script - 9/9/2005 2:56:51 AM   
  kracksmith

 

Posts: 198
Score: 0
Joined: 2/24/2005
From:
Status: offline
ok it works now. the "shell" needed to be DIM and there was a typo "oShell" that needs to be changed to just "Shell"


Option Explicit
Dim oFS, oWsh, oNetwork, strUser, Shell
Dim DesktopPath, link
Dim folder, file, icon1, icon2, icon3, icon4

Set oWsh = CreateObject("WScript.Shell")
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Set oNetwork= CreateObject("Wscript.Network")
Set Shell = CreateObject("WScript.Shell")

 
strUser = oNetwork.UserName
 
DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\Paper List.lnk")
link.TargetPath = "
\\Server1\Local Drive\Storage\house\paper.xls"
link.Save


icon1 = UCase(oWsh.SpecialFolders("Desktop") & "\PAPER.XLS.lnk")
icon2 = UCase(oWsh.SpecialFolders("Desktop") & "\Shortcut to PAPER.lnk")
icon3 = UCase(oWsh.SpecialFolders("Desktop") & "\PAPER.lnk")
icon4 = UCase(oWsh.SpecialFolders("Desktop") & "\ABC PAPER LIST.XLS.lnk")

Set folder = oFS.GetFolder(DesktopPath)
For Each file In folder.Files
Select Case UCase(file)
Case icon1
oFS.DeleteFile(file)
Case icon2
oFS.DeleteFile(file)
Case icon3
oFS.DeleteFile(file)
Case icon4
oFS.DeleteFile(file)
Case Else
End Select
Next
WScript.Quit (0)

(in reply to kracksmith)
 
 
Post #: 15
 
 RE: desktop deleting some file script - 9/9/2005 4:38:52 PM   
  binHEX


Posts: 9
Score: 1
Joined: 9/2/2005
Status: offline
My bad.  I preach at you for not DIM'ing your variables, an then I turn around and do the same thing.

(in reply to kracksmith)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> desktop deleting some file script Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts