Free Power Tools Brochure
Get Mark Minasi's
17-page guide today!



      

advertisement

Get Newsletters

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

Subscribe Now!

March 19, 2001 06:19 PM

Command Output in a Variable

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

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

ARTICLE TOOLS

Add a Comment

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 2/5/2004 10:12:33 AM


You must log on before posting a comment.

Are you a new visitor? Register Here

Setup rights to helpdesk group to unlock shared files

Does anyone know how to setup a group or permissions for a group (helpdesk) to be able to unlock a shared network file such as a spreadsheet or Access...222-96217

advertisement

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS

Podcasts

To successfully implement virtual desktops, IT administrators must carefully match user requirements to specific desktop technologies. Listen to this podcast to learn what you need to keep in mind when formulating your approach to desktop virtualization.

Downloads

PacketTrap IT is a comprehensive and affordable network management and application monitoring solution that solves problems associated with bandwidth, network and application performance, and connectivity. Gain insight into your network - try PacketTrapIT free for 21 days!

Web Seminars

Aside from its employees, data is an organization’s most important resource. Join Windows technical specialist and 11-time MVP John Savill to learn the best practices for managing data using features in Windows Server.
View this web seminar on demand!

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.