+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Need a hand with some vbs in Coding and Web Development; ...
  1. #1

    Join Date
    Dec 2007
    Location
    Nottinghamshire
    Posts
    187
    Thank Post
    60
    Thanked 21 Times in 19 Posts
    Rep Power
    17

    Need a hand with some vbs

    This vbs script is used to remove files from a network share after they havenít been modified for so long. The script its self works fine I just need to change it to record what the script is deleting.


    The original code

    ' removes files that are longer than the day are eg. "c:\script name" "delete location" "number of days"
    Set objArgs = WScript.Arguments
    FolderName = objArgs(0)
    Days = objArgs(1)

    set fso = createobject("scripting.filesystemobject")
    set folders = fso.getfolder(FolderName)
    datetoday = now()
    newdate = dateadd("d", Days*-1, datetoday)
    recurse folders
    sub recurse( byref folders)
    set subfolders = folders.subfolders
    set files = folders.files
    for each file in files
    if file.datelastmodified < newdate then
    on error resume next
    file.delete
    end if

    next

    for each folder in subfolders
    recurse folder
    next

    set subfolders = nothing
    set files = nothing

    end sub
    I tried to change it to this it still removes the files but doesnít write to the file but I canít see what Iíve done wrong.


    ' removes files that are longer than the day are eg. "c:\script name" "delete location" "number of days" ďc:\log fileĒ
    Set objArgs = WScript.Arguments
    FolderName = objArgs(0)
    Days = objArgs(1)
    logpath = objArgs(2)

    set fso = createobject("scripting.filesystemobject")
    set OutputFile = fso.OpenTextFile(logpath)
    set folders = fso.getfolder(FolderName)
    datetoday = now()
    newdate = dateadd("d", Days*-1, datetoday)
    recurse folders
    sub recurse( byref folders)
    set subfolders = folders.subfolders
    set files = folders.files
    for each file in files
    if file.datelastmodified < newdate then
    on error resume next
    OutputFile.Write file
    OutputFile.close
    file.delete
    end if

    next

    for each folder in subfolders
    recurse folder
    next

    set subfolders = nothing
    set files = nothing

    end sub
    I would love it if someone could show me what I did wrong or even point me to somewhere that will help me see what I did wrong.

  2. #2
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,525
    Thank Post
    106
    Thanked 89 Times in 75 Posts
    Blog Entries
    46
    Rep Power
    40
    Quote Originally Posted by notalot View Post
    This vbs script is used to remove files from a network share after they havenít been modified for so long. The script its self works fine I just need to change it to record what the script is deleting.


    The original code



    I tried to change it to this it still removes the files but doesnít write to the file but I canít see what Iíve done wrong.



    I would love it if someone could show me what I did wrong or even point me to somewhere that will help me see what I did wrong.
    not much wrong. I changed the arguments to variables to make diagnostics easier.
    with these alterations it worked for me, running from d:\temp\new folder

    stuff I've changed / added

    FolderName = "d:\temp\new folder"
    Days = 5
    logpath = "d:\temp\new folder\log.txt"

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    set OutputFile = fso.OpenTextFile(logpath, forwriting, true)

  3. Thanks to browolf from:

    notalot (18th March 2009)

  4. #3

    Join Date
    Dec 2007
    Location
    Nottinghamshire
    Posts
    187
    Thank Post
    60
    Thanked 21 Times in 19 Posts
    Rep Power
    17
    Thats perfect i see where i got it wrong, so easy to make errors in vbs

SHARE:
+ Post New Thread

Similar Threads

  1. [Video] Hand up who had one of these.....
    By mattx in forum Jokes/Interweb Things
    Replies: 3
    Last Post: 26th September 2008, 03:15 PM
  2. Refurb/Second Hand dealers
    By wesleyw in forum Hardware
    Replies: 3
    Last Post: 22nd September 2008, 09:42 AM
  3. Second hand Mac Wanted
    By FN-GM in forum Mac
    Replies: 5
    Last Post: 7th September 2007, 12:10 PM
  4. Buying Second-hand kit
    By SimpleSi in forum Hardware
    Replies: 13
    Last Post: 18th June 2007, 11:07 AM
  5. Printers- Second Hand
    By ticker in forum Hardware
    Replies: 9
    Last Post: 3rd May 2006, 04:04 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
  •