Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 08, 2006

Learn How to Error-Proof Paths and Creatively Manipulate Strings

And you even get a script that enumerates user profiles to boot
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

As Figure 1 shows, the NetUsers output contains a lot of extraneous lines. You need only the lines with the user information—in this case, the lines that start with MyDom\ fredsmith and MyDom\donnawong. To filter the NetUsers output so that you get only the lines that contain the user information, you can use the Find command with the /V switch. This switch tells the Find command to display all lines that don't contain the specified text. So, in this example, you use the command

NetUsers.exe \\work1 /h
  Find /V " -"| 
  Find /V "successfully."| 
  Find /V "History"| 
  Find /V "Connecting" 

Figure 2 shows the output from this command. You now have only the lines you need (aka the filtered strings), but there's another problem. The filtered strings contain varying numbers of spaces, which creates problems when you try to embed the strings in a For command. So, you need to do some creative coding to retrieve the four parts—the user ID (i.e., Domain\UserID), the friendly name, and the date and time of last logon—of each filtered string.

Assuming that the user IDs don't contain any spaces, the first part of the filtered string will always be the Domain\UserID. Thus, you can use the For command to break the filtered string into pieces, or tokens, so you can retrieve the user ID. As callout E shows, GetUserProfiles.bat captures the user ID in the first token (%i variable) and assigns the rest of the filtered string to the Remainder variable.

You can't use tokens to retrieve the friendly name because the friendly name might include a middle initial or might consist of several elements separated by spaces (e.g., Le Pu, Pepe), which means the friendly name might be in two or more tokens. For example, if the user has a name with no middle initial (e.g., Smith, Fred), the date would be in the fourth token (%l variable). If the user has a middle initial (e.g., Wong, Donna J.), the date would be in the fifth token (%m variable).

Fortunately, you can perform some advanced string manipulations to get the friendly name and the date and time information. You're probably familiar with using the Set command to perform environment-variable string substitutions. For example, the command

Set %PATH:str1=str2% 

expands the PATH environment variable, then replaces the string on the left side of the equal sign (str1) with the string on the right side of the equal sign (str2).

A less commonly used environment-variable string substitution lets you expand an environment variable and extract a specific number of characters from the end of the expanded string. For example, to expand the PATH environment variable and extract the last 5 characters from the end of the expanded string, you use the command

Set %PATH:~-5% 

You can use this type of string substitution to extract the date and time information from the Remainder variable. (You can also use this type of string substitution to extract characters from the beginning of a string. For more information about this substitution, run the Set command with the /? switch.) As Figure 2 shows, the time is the last 5 characters in a filtered string and the date is characters 6 through 16 if you count backward from the end of a filtered string. Thus, you can use the code that callout F highlights to extract the date and time.

With the user ID and date and time information retrieved from the filtered string, all that remains is the retrieval of the friendly name. As callout G shows, you can use a series of string replacements to retrieve the friendly name.

Note that there is always more than one way to handle a task in a script. Another way to retrieve the friendly name would be to use environment-variable string substitution to skip over the last 16 characters in the Remainder variable's string, assign the resulting substring a variable, then remove any double spaces from the substring.

   Previous  1  [2]  3  Next 


Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...


Task Automation Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement