Windows 2000 VBS Scripting

Posted on 2006-04-19
Last Modified: 2008-01-09
First, I don't know what I'm doing when it comes to scripting, which is why I am asking for help.

On a Windows 2000 Pro machine with all SPs and updates installed, I downloaded a VBS script which is supposed to run dfrg.msc and enter key commands to defrag the primary partition with no user intervention. Scheduled Tasks indicates that it has been running as scheduled, however the volume is not getting defragged. So I manually entered each command in the VBS script in order at the command line to determine where it is going wrong.

The second command in the script, "WshShell.Run "dfrg.msc"", is generating the error "wshShell.Run is not recognized as an internal or external command".

Is it possible to get this script to run on this machine?
Question by:westone
    LVL 44

    Expert Comment

    Hello westone,

    according to this thread on the same vbs it will not work on windows 2000

    however there is a post to a windows 2000 solution
    If you want something that works for Win2k in a scheduled task:

    AutoDeFrag is a free command line based wrapper of the Win2k defrag command

    hope this helps a bit

    Author Comment

    Thanks for link. Autodefrag is not working either. I have a dozen identical W2000 Pro machines, and neither the VBS script nor the Autodefrag executable work on any of them.
    LVL 44

    Expert Comment

    i'm not at a w2k server now so no way to test this

    but from the looks of the vbs files there it should be worth giving a go

    Expert Comment

    This is the code I use on W2K and it works for me.  Contents of defrag.vbs:

    set WshShell = CreateObject("WScript.Shell")

    'Launch Defrag from the command line and wait for 2 seconds
    WshShell.Run "dfrg.msc"
    WScript.Sleep 2000

    'Wait until the application has loaded - Check every second
    While WshShell.AppActivate("Disk Defragmenter") = FALSE
          wscript.sleep 1000

    '1 Bring the application to the foreground
    WshShell.AppActivate "Disk Defragmenter"
    WScript.Sleep 500

    '1 Send keys to move to top of drive/partition table.
    WshShell.SendKeys "{TAB}{DOWN}"
    WScript.Sleep 501

    '1 Send an ALT-A key to bring down the defrag menu
    WshShell.SendKeys "%A"
    WScript.Sleep 502

    '1 Send a D to start the defrag
    WshShell.SendKeys "D"

    '1 Wait until the defrag is completed - Check for window every 5 seconds
    While WshShell.AppActivate("Defragmentation Complete") = FALSE
          wscript.sleep 5001

    '1 Bring the Defragmentation Complete window to the foreground
    WshShell.AppActivate "Defragmentation Complete"
    WScript.Sleep 503

    '1 Send a tab key to move the focus from View Report button to the Close Button
    WshShell.Sendkeys "{TAB}"
    Wscript.Sleep 504

    '1 Send key to Close the Defragmentation Complete window
    WshShell.Sendkeys "{ENTER}"
    Wscript.Sleep 505

    'End - Bring the application to the foreground
    WshShell.AppActivate "Disk Defragmenter"
    WScript.Sleep 700

    'End - Send an ALT-F4 to Close the Defrag program
    WshShell.Sendkeys "%{F4}"

    Author Comment

    That's the same script I downloaded from the net that was stated to work under Windows 2000. I downloaded it from the compu-docs link (same one bruintje provided earlier). It just doesn't work.

    Is it correct that entering each command in the script at the command line should work same as if the script were running?
    LVL 14

    Expert Comment

    No, absolutely not.  Lines of a batch file can be entered on a command line, but VBScript is interpreted by cscript.exe or wscript.exe, not the command shell.
    LVL 2

    Accepted Solution

    You have to put the script in a file with .vbs extension and then execute the file...
    (by clicking on it or running "wscript.exe <filename>.vbs" from command line)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A short article about problems I had with the new location API and permissions in Marshmallow
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now