VBS script to copy, move, and then delete folders based on age

Posted on 2007-10-18
Last Modified: 2008-06-26
I am looking for a VB script that I can use to copy folders from one server to another, and then delete the folders from there original location based on the folders age. I've been looking around the web and am getting a bit lost. Does anyone have some code they can share with a beginner?

Question by:fyrfyter
    LVL 29

    Expert Comment

    Use the FileSystemObject. It is designed specifically for this type of activity.
    LVL 3

    Accepted Solution

    This is a script to retrieve all porperties from a folder:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService. _
        ExecQuery("Select * from Win32_Directory where name = 'c:\\Scripts'")
    For Each objFolder in colFolders
        Wscript.Echo "Archive: " & objFolder.Archive
        Wscript.Echo "Caption: " & objFolder.Caption
        Wscript.Echo "Compressed: " & objFolder.Compressed
        Wscript.Echo "Compression method: " & objFolder.CompressionMethod
        Wscript.Echo "Creation date: " & objFolder.CreationDate
        Wscript.Echo "Encrypted: " & objFolder.Encrypted
        Wscript.Echo "Encryption method: " & objFolder.EncryptionMethod
        Wscript.Echo "Hidden: " & objFolder.Hidden
        Wscript.Echo "In use count: " & objFolder.InUseCount
        Wscript.Echo "Last accessed: " & objFolder.LastAccessed
        Wscript.Echo "Last modified: " & objFolder.LastModified
        Wscript.Echo "Name: " & objFolder.Name
        Wscript.Echo "Path: " & objFolder.Path
        Wscript.Echo "Readable: " & objFolder.Readable
        Wscript.Echo "System: " & objFolder.System
        Wscript.Echo "Writeable: " & objFolder.Writeable

    And this is for moving it:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
        ("Select * from Win32_Directory where name = 'c:\\Scripts'")
    For Each objFolder in colFolders
        errResults = objFolder.Rename("C:\Admins\Documents\Archive\VBScript")
        Wscript.Echo errResults

    Author Comment

    Thanks elissaqp. I think this is what I was looking for. Any tips for running it as a scheduled task?

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now