CMD Master
Back to Blog
UpdatedArnošt Havelka

Variables and Time

Use environment variables and time commands to build predictable automation and timestamped logs.

Start Interactive Lesson
Variables and Time

Variables and timestamps are the backbone of maintainable batch workflows. Without them, scripts hardcode paths, logs become inconsistent, and debugging gets painful.

Core Idea

Use variables for values that change between machines or runs, and use time values to stamp outputs so every run is traceable.

Try the command

How do you print the PATH variable?

Build the command
set NAME=valueCreate/update variable for current shell session.
%NAME%Resolve the variable value in commands.
%DATE% %TIME%Emit execution timestamp for logs and reports.
Terminal
C:\Users\Student>echo %PATH%
C:\Windows\System32;C:\Windows

Step 1: Read Existing Environment Values

Terminal
C:\Users\Student>echo %PATH%

This confirms what executables your session can find without full paths.

Step 2: Build a Timestamped Log Entry

Terminal
C:\Users\Student>echo Backup started at %DATE% %TIME% >> backup.log

Appending date/time entries makes audits and rollback investigations much faster.

Reliable Pattern

  • Use set for dynamic directories and environment-specific values.
  • Use %DATE% and %TIME% in every operational log line.
  • Avoid hardcoded paths when scripts will run on multiple machines.

This single pattern greatly improves script portability and incident traceability.

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

How to Check Your Windows User and Admin Groups with whoami

Use whoami, whoami /groups, and whoami /priv in CMD to see the account, groups, and privileges a script is actually running with.