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


April 2001

Command Output in a Variable

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

I use command scripts to perform various administrative tasks—generating files that contain date- or time-related data is often useful. A simple way to generate a filename that includes date or time information is to include the following lines in batch files:

For /F "Tokens=2" %%I in ('Date /T') Do Set StrDate=%%I
For /F "Tokens=*" %%I in ('Time /T') Do Set StrTime=%%I

These commands assign the date to a variable, %StrDate%, and the time to a variable, %StrTime%, which you can use for subsequent batch processing, as the following command shows:

Dir C:\*.* /S /B >> %Temp%\%Computername%_%StrDate%%StrTime%.Log

By changing the For statement's syntax, you can adjust the %StrDate% and %StrTime% variables so that you can get the format of information you require.

You can also use this method to assign any type of command output to a variable:

For /F "Tokens=2" %%I in ('<any command>') Do Set <variable name>=%%I

For example, the following command redirects the output of a Ping command to %StrServer% in the variable %StrAnswer%:

For /F "Skip=3 Tokens=*" %%I in ('ping %StrServer% -n 1') Do Set StrAnswer=%%I

End of Article



Reader Comments
InstantDoc #20029 The script won't run. The value of the Variable StrTime has colons in it, which are not allowed in filenames. Try this (in batch file):
For /F "tokens=1,2,3 delims=:" %%I in ('now') Do set FileName=%%I.%%J.%%K

Bruce Heimbigner February 05, 2004


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Microsoft Warns of Windows Version Expirations

Microsoft warned that this year will see three out-of-date Windows versions slip into retirement. ...


Scripting Whitepapers Three Ways to Prevent Insider Risk within Your SCCM-Managed Environment

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

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting 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.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement