Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

March 19, 2001 12:00 AM

Command Output in a Variable

Windows IT Pro
InstantDoc ID #20029
Rating: (1)

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

Related Content:

ARTICLE TOOLS

Comments
  • Bruce Heimbigner
    8 years ago
    Feb 05, 2004

    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

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.