Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


July 30, 1999

Rem


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Do you have a scripting-related question or problem? You can send your question or problem to win32sj@winntmag.com.

Q: Do the names of script variables have some relevance, or are they purely arbitrary?

You can use whatever name you like for a variable. However, the consensus is to use a prefix with a descriptive name.

The prefix, which represents the type of data, typically contains one lowercase character or three lowercase characters. Commonly used three-character prefixes include

  • str = String
  • int = Integer
  • bol = Boolean
  • obj = Object
  • ads = ADSI object
  • app = Application object
  • wsh = WSH object
  • arr = Array
  • ado = ADO object
  • fso = FolderSystemObject
  • fil = File object
  • fol = Folder object
  • lgn - Long Integer
  • sgl = Single precision value
  • dbl = Double precision value

In the descriptive name, you typically capitalize the first letter of each word but you don't put hyphens between words. (Typically, variable names are case-insensitive.) For example, you can name a string variable strMyPassword.

Q: In Windows Scripting Host (WSH), you can create a shortcut, but how can you read the properties of an existing shortcut?

You can't. The WSH object model control (wshom.ocx) doesn't expose wshShortcut as a full automation object. You can expose wshShortcut only through wshShell.CreateShortcut.

Q: How can I use WSH to write a script that checks the size of Windows NT and Windows 95 files and deletes those files that are larger than a specified size?

Listing 1 contains an example of how you might use WSH, VBScript, and the Scripting Runtime Library's FileSystemObject to check and delete files. You begin the script by declaring the variables and defining a constant that specifies the maximum file size. Although this script sets the constant's value to 1MB, you can change the constant's value to meet your needs.

Next, you initialize strFileSpec to the target filename, which you customize. You then create a reference to FileSystemObject, which you subsequently use to create a reference to the target file. You use the target file reference to access the File object's Size property and Delete method. The WScript.Echo statement displays the file's current size in megabytes, kilobytes, and bytes.

You use the If...Then statement to compare the file's size in bytes to the constant you defined earlier. If the file size is greater than 1MB, you delete the file.

Q: How can I create an NT shell script that deletes files with a date older than MMDDYY from a directory?

To delete files by date, you need to install the Microsoft Windows NT Server 4.0 Resource Kit's Robocopy utility. Robocopy has a switch that lets you specify whether you want to copy or move files that are older than a certain age. You then take these steps:

  1. Set up a directory in which to put the old files that you want to delete. Name the directory OldFiles or a similar intuitive name. Robocopy will move users' old files into this OldFiles destination folder.
  2. Write a script that first deletes all the files and folders in the OldFiles folder and then calls Robocopy. Listing 2 contains a script that you can adapt. In your script, have Robocopy move all the files that are older than a specified number of days (e.g., 120 days) or a specified date (e.g., 070199) from the source directory to the OldFiles folder. Robocopy automatically clears the source area of any old files, and you can use the /move switch instead of the /mov switch to clear any empty directories.
  3. Schedule the script to run at a specified interval with the Net Use command.

Keeping users' old files in the OldFiles folder until the next scheduled deletion run rather than immediately deleting them can save you work down the road. If users need a file that Robocopy removed, you can restore the file from the OldFiles folder rather than from the backup tape.

Robocopy is powerful and can do real damage if you get the switches wrong. However, if you use Robocopy carefully, it will become another valuable addition to your NT toolkit.

Q: In "An ADSI Primer, Part 5: Using ADO to Script Fast ADSI Searches" (May 1999), Alistair G. Lowe-Norris discusses the ADO database connector for Active Directory Service Interfaces (ADSI). Where can I obtain this ADO OLE DB connector?

You can find the ADO OLE DB connector on the Microsoft Developer Network (MSDN). Go to http://msdn.microsoft.com/developer/ sdk/platform.asp, and select MDAC 2.1 (including ADO, ODBC, and OLE DB). Make sure that you have the most recent copy of Microsoft Data Access (MDAC).

End of Article



Reader Comments
In your article 'How can I create an NT shell script that deletes files with a date...' you say that Robocopy utility 'has a switch that lets you specify whether you want to copy or move files that are older than a certain age...' I am sorry, but in the documentation included with the utility I didn't found any switch about 'date'. Is it wrong?
Thanks in advance.

Roberto September 08, 1999


I do not see any date commands in Robocopy. Do you know what they are?

glenn hunsberger December 28, 2000


On the subject:
Q: In Windows Scripting Host (WSH), you can create a shortcut, but how can you read the properties of an existing shortcut?

You can't. The WSH object model control (wshom.ocx) doesn't expose wshShortcut as a full automation object. You can expose wshShortcut only through wshShell.CreateShortcut.

My comment: I was as frustrated by this as the original questioner probably was. However, I've found a way round it, even though it is 'dirty'. Do the following: 1) Get hold of the existing shortcut with fso.GetFile. 2) Create a new shortcut with wshShell.CreateShortcut and save it with .Save. 3) Copy the existing shortcut over the new one, using the Copy command, with overwrite set to True. 4) Now .targetPath, .Arguments, .WorkingDirectory, etc are available to your new shortcut object.
Sample:
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ExistLink = fso.GetFile("existing-shortcut-filename")
Set newLink = wshShell.CreateShortcut("new-shortcut-filename")
newLink.Save
existLink.Copy "new-shortcut-filename", True
msgbox "TargetPath=" & newLink.TargetPath
msgbox "Desc=" & newLink.Description
msgbox "FullName=" & newLink.FullName
msgbox "IconLocation=" & newLink.IconLocation
msgbox "RelativePath=" & newLink.RelativePath
msgbox "WindowStyle=" & newLink.WindowStyle
msgbox "WorkingDirectory=" & newLink.WorkingDirectory

As this page was the earliest one to be helpful in my search, I feel that you should benefit from being able to host this workaround.

Ian Sanders October 23, 2003


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
VMware and the Future of Virtualization

What's next for virtualization and business IT? Windows IT Pro senior editor Jeff James speaks with VMware President and CEO Diane Greene on the future of virtualization technology. ...

WinInfo Short Takes: Week of September 8, 2008

An often irreverent look at some of the week's other news, including the long-awaited back to school season, Microsoft's first Seinfeld/Gates ad, some EU insights, another Netbook improvement, Opera silliness, and much, much more ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

IT Connections
Dive into the new Microsoft platforms and products you implement and support with the experts from Microsoft, TechNet Magazine, Windows ITPro and industry gurus. There are 70+ sessions and interactive panels with networking opportunities.

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes and add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Get SQL Server 2008 at WinConnections
Don’t miss Microsoft Exchange and Windows Connections conferences, the premier events for Microsoft IT Professionals in Las Vegas, November 10-13. Every attendee will receive a copy of SQL Server 2008 Standard Edition with one CAL.



Interested in Email Encryption?
Read about the advantages of identity-based encryption in this free report.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing