+ Post New Thread
Results 1 to 2 of 2
Scripts Thread, VBS Scribt in Coding and Web Development; Hi Guys Im new to this forum and just started my new job, I have been working on abit of ...
  1. #1

    Join Date
    Apr 2009
    Location
    ENGLAND
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Exclamation VBS Scribt

    Hi Guys Im new to this forum and just started my new job, I have been working on abit of VBS scribt to delete files older than 2 days in a shared directory. It has to remove all files and folder in 1 particular directory called C:\share48. Problem being is that I get a dialog box which I dont want popping up. Secondly it also removes the shre48 folder which i dont want it to, its supposed to delete all files and folders below it.

    This is what i came up with:

    _______________________________________

    Dim oFSO, oFolder, oFiles, oFile
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder("c:\share48\test")
    Set oFiles = oFolder.Files
    for each oFile in oFiles
    if oFile.DateLastModified > DateAdd("d", 2, Now()) then

    oFile.Delete True
    end if
    next


    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name = 'c:\\share48'")
    For Each objFolder in colFolders
    errResults = objFolder.Delete
    Wscript.Echo errResults

    next

    or use this script but it also deleted the c:\share48 folder which I dont whant it to.

    strPath = "C:\share48"

    DeleteFolder strPath

    Function DeleteFolder(strFolderPath)
    Dim objFSO, objFolder
    Set objFSO = CreateObject ("Scripting.FileSystemObject")
    If objFSO.FolderExists(strFolderPath) Then
    objFSO.DeleteFolder strFolderPath, True
    End If
    Set objFSO = Nothing
    End Function


    __________________________________________________ _________

    Please Help ^^

    cheers guys.
    Last edited by RobPrince; 6th April 2009 at 12:13 PM.

  2. #2

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,156
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    You seem to have got several scripts mixed up. the first chunk should work just fine although your dateadd is the wrong way round ("old" dates are smaller numbers)

    The message box popping up is probably because you're running this with wscript instead of cscript - either type "cscript nameofscript.vbs" or change to use cscript as default (cscript //h:cscript)

    Code:
    Dim oFSO, oFolder, oFiles, oFile
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder("c:\share48\test")
    Set oFiles = oFolder.Files
    for each oFile in oFiles
    if oFile.DateLastModified < DateAdd("d", -2, Now()) then
    
    oFile.Delete True
    end if
    next

SHARE:
+ Post New Thread

Similar Threads

  1. vbs logon with ie
    By box_l in forum Scripts
    Replies: 5
    Last Post: 11th August 2010, 11:35 PM
  2. A little .vbs help please
    By ozzy in forum Scripts
    Replies: 9
    Last Post: 27th February 2009, 08:07 AM
  3. small vbs help please
    By RabbieBurns in forum Scripts
    Replies: 4
    Last Post: 20th May 2008, 03:30 PM
  4. Sub within a sub - VBS Script
    By FN-GM in forum Scripts
    Replies: 5
    Last Post: 18th May 2008, 06:30 PM
  5. Would like to learn VBS
    By bishopsgarthstockton in forum Web Development
    Replies: 11
    Last Post: 12th July 2007, 07:44 AM

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
  •