+ Post New Thread
Results 1 to 5 of 5
Windows Thread, VBScript, wildcards or file extensions in Technical; I need a script that can delete either file extensions or a wildcard? Basically due to R2 not being able ...
  1. #1

    Join Date
    Apr 2007
    Location
    Bournemouth
    Posts
    72
    Thank Post
    13
    Thanked 4 Times in 2 Posts
    Rep Power
    16

    VBScript, wildcards or file extensions

    I need a script that can delete either file extensions or a wildcard? Basically due to R2 not being able to set a default printer we still use a vbs in the all users startup folder to set the default after login. Problem is these vbs files and the startup folder is populating the recent documents folder and I was hoping to add to the vbs default printer script at the end to delete *.vbs files and the startup link.

    Is this possible? Can you delete file extensions in a VBScript?

    Thanks guys, have done an extensive search but with no avail.

  2. #2
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,995
    Thank Post
    120
    Thanked 286 Times in 263 Posts
    Rep Power
    108
    Why dont you just assign it as a log on script?

  3. #3

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,157
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    I'd use a logon script but if you want to clear particular links then it's possible but not quite as easy as you want - all the "recent" files have a .lnk extension so this code won't work:
    Code:
    set ofso=createobject("scripting.filesystemobject")
    set oShell=createobject("wscript.shell")
    sRecent=oshell.specialfolders("recent")
    set oFolder=ofso.getfolder(sRecent)
    for each oFile in oFolder.files
      sExtension=lcase(ofso.getextensionname(oFile))
      wscript.echo sExtension, oFile.name
      if sExtension="vbs" then oFile.delete  
    next
    All is not lost, however, because you can just use a bit more code:
    Code:
    set ofso=createobject("scripting.filesystemobject")
    set oShell=createobject("wscript.shell")
    sRecent=oshell.specialfolders("recent")
    set oFolder=ofso.getfolder(sRecent)
    for each oFile in oFolder.files
      sName=lcase(ofso.getbasename(oFile))
      if right(sName,3)="vbs" then oFile.delete
    next
    Instead of just directly getting the file extension (the bit after the last full stop) you take the bit before the last full stop (basename) and check the last 3 characters.

    This is relatively inefficient; if there are 1000 files in a folder it will look at all of them but because it runs on the local PC and only runs once per session it's probably OK.

    If you really do want to do wildcard searches then you have to use WMI and the CIM_DataFile:

    Code:
    sComputer = "."
    set oShell=createobject("wscript.shell")
    sRecent=oshell.specialfolders("recent")
    if mid(sRecent,2,1)<>":" then
      wscript.echo "Can't find recent; giving up"
      wscript.quit
    end if
    sDrive=left(sRecent,2)
    sRecent=replace(mid(sRecent,3),"\","\\") & "\\"
    
    Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
    
    sSQL="Select * from CIM_DataFile Where drive='" & sDrive & "' and (name like '%.vbs.%') and (path='" & sRecent &"')" 
    
    Set colFiles = oWMIService.ExecQuery(sSQL)
    
    For Each oFile in colFiles
        oFile.delete
    Next
    This is a bit more complicated (!) but is much more flexible. Key things is that it needs to know which computer you are looking at ("." means local computer); you have to know the drive letter (so it gives up if the recent path doesn't start x: - I could strip off the PC name from the start of the share but life's too short!), you have to escape \ with an extra \ and finally, the wildcard is the % sign. Other than that, it's really easy :-)

    You can run this against something like an entire fileserver to search for files but it does seem to be rather memory hungry - I'd guess it builds the file collection in memory.

  4. Thanks to srochford from:

    Bobo (16th April 2008)

  5. #4

    Join Date
    Apr 2007
    Location
    Bournemouth
    Posts
    72
    Thank Post
    13
    Thanked 4 Times in 2 Posts
    Rep Power
    16
    Quote Originally Posted by ChrisH View Post
    Why dont you just assign it as a log on script?
    Because the startup folder comes into action after login so the printer will end up right back in the recent documents folder.

    Thanks srochford, although I was worried about the .lnk extensions bit, but thought there may be some way as you suggested. I have avoided learning vbs like the plague and rely on "thieving" others scripts so I thank you. We are cutting the folder down to a size of around 30 so the searching should not take too long.

    Again much thanks, and I will no doubtedly ask for more advice before this problem is solved.

  6. #5

    Join Date
    Aug 2011
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Smile

    Quote Originally Posted by Bobo View Post
    I need a script that can delete either file extensions or a wildcard? Basically due to R2 not being able to set a default printer we still use a vbs in the all users startup folder to set the default after login. Problem is these vbs files and the startup folder is populating the recent documents folder and I was hoping to add to the vbs default printer script at the end to delete *.vbs files and the startup link.

    Is this possible? Can you delete file extensions in a VBScript?

    Thanks guys, have done an extensive search but with no avail.
    This will delete all files in the last nested folder with extension txt. Plain and simple.

    Const DeleteReadOnly = TRUE 'or FALSE, if you like
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile("C:\folder\folder\...\folder\*.t xt"), DeleteReadOnly



SHARE:
+ Post New Thread

Similar Threads

  1. IIS 5 and web extensions
    By fafster in forum How do you do....it?
    Replies: 0
    Last Post: 8th April 2008, 10:11 AM
  2. File Extensions
    By Gatt in forum Windows
    Replies: 5
    Last Post: 31st January 2007, 02:29 PM
  3. Replies: 5
    Last Post: 12th January 2007, 10:26 PM
  4. Frontpage Extensions
    By faza in forum Web Development
    Replies: 4
    Last Post: 4th April 2006, 11:19 AM
  5. Replies: 1
    Last Post: 14th February 2006, 01:31 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •