Skip navigation

Category Archives: shell

Windows shell scripting

fantastic

The %stuff%

There are several items that you can use that looks like %this%.

  • %PATH%
  • %APPDATA%
  • %DATE%
  • %TIME%

basically, these %this% is how you access any of the environment variables defined on your system. Right click My Computer > Properties > Advanced > Environment Variables

Those are all the environment variables on your system.

At CMD:

C:\> echo %APPDATA%

HKLM local machine variables (bottom half) supercede current user variables (top half). Well, if you have a PATH variable defined as both HKLM and HKCU then if you echo %PATH% it gives you the HKLM machine variable; CONTENATE HKCU variable

Substring: %VAR:offset,len%

e.g.

echo %DATE:~1,2%    REM If its Dec, gives ec.  Starting at position 1, get 2 chars
echo %DATE:~1%      REM Goes from position 1 to end of string.  ec 27 08

Above example not that useful, but at least its clear how to work it

Getting random user input.

Try putting this into test.bat, then running it:

set /P MyAnswer=Tell me something: 

echo You said: "%MyAnswer%"

pause

Thanks to this guy again for that neat tip!